You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
16KB

  1. /*
  2. * Dirac encoder support via Schroedinger libraries
  3. * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Dirac encoder support via libschroedinger-1.0 libraries. More details about
  24. * the Schroedinger project can be found at http://www.diracvideo.org/.
  25. * The library implements Dirac Specification Version 2.2
  26. * (http://dirac.sourceforge.net/specification.html).
  27. */
  28. #undef NDEBUG
  29. #include <assert.h>
  30. #include <schroedinger/schro.h>
  31. #include <schroedinger/schrodebug.h>
  32. #include <schroedinger/schrovideoformat.h>
  33. #include "avcodec.h"
  34. #include "internal.h"
  35. #include "libschroedinger.h"
  36. #include "bytestream.h"
  37. /** libschroedinger encoder private data */
  38. typedef struct SchroEncoderParams {
  39. /** Schroedinger video format */
  40. SchroVideoFormat *format;
  41. /** Schroedinger frame format */
  42. SchroFrameFormat frame_format;
  43. /** frame being encoded */
  44. AVFrame picture;
  45. /** frame size */
  46. int frame_size;
  47. /** Schroedinger encoder handle*/
  48. SchroEncoder* encoder;
  49. /** buffer to store encoder output before writing it to the frame queue*/
  50. unsigned char *enc_buf;
  51. /** Size of encoder buffer*/
  52. int enc_buf_size;
  53. /** queue storing encoded frames */
  54. FFSchroQueue enc_frame_queue;
  55. /** end of sequence signalled */
  56. int eos_signalled;
  57. /** end of sequence pulled */
  58. int eos_pulled;
  59. /* counter for frames submitted to encoder, used as dts */
  60. int64_t dts;
  61. } SchroEncoderParams;
  62. /**
  63. * Works out Schro-compatible chroma format.
  64. */
  65. static int set_chroma_format(AVCodecContext *avccontext)
  66. {
  67. int num_formats = sizeof(schro_pixel_format_map) /
  68. sizeof(schro_pixel_format_map[0]);
  69. int idx;
  70. SchroEncoderParams *p_schro_params = avccontext->priv_data;
  71. for (idx = 0; idx < num_formats; ++idx) {
  72. if (schro_pixel_format_map[idx].ff_pix_fmt ==
  73. avccontext->pix_fmt) {
  74. p_schro_params->format->chroma_format =
  75. schro_pixel_format_map[idx].schro_pix_fmt;
  76. return 0;
  77. }
  78. }
  79. av_log(avccontext, AV_LOG_ERROR,
  80. "This codec currently only supports planar YUV 4:2:0, 4:2:2"
  81. " and 4:4:4 formats.\n");
  82. return -1;
  83. }
  84. static int libschroedinger_encode_init(AVCodecContext *avccontext)
  85. {
  86. SchroEncoderParams *p_schro_params = avccontext->priv_data;
  87. SchroVideoFormatEnum preset;
  88. /* Initialize the libraries that libschroedinger depends on. */
  89. schro_init();
  90. /* Create an encoder object. */
  91. p_schro_params->encoder = schro_encoder_new();
  92. if (!p_schro_params->encoder) {
  93. av_log(avccontext, AV_LOG_ERROR,
  94. "Unrecoverable Error: schro_encoder_new failed. ");
  95. return -1;
  96. }
  97. /* Initialize the format. */
  98. preset = ff_get_schro_video_format_preset(avccontext);
  99. p_schro_params->format =
  100. schro_encoder_get_video_format(p_schro_params->encoder);
  101. schro_video_format_set_std_video_format(p_schro_params->format, preset);
  102. p_schro_params->format->width = avccontext->width;
  103. p_schro_params->format->height = avccontext->height;
  104. if (set_chroma_format(avccontext) == -1)
  105. return -1;
  106. if (avccontext->color_primaries == AVCOL_PRI_BT709) {
  107. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
  108. } else if (avccontext->color_primaries == AVCOL_PRI_BT470BG) {
  109. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
  110. } else if (avccontext->color_primaries == AVCOL_PRI_SMPTE170M) {
  111. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
  112. }
  113. if (avccontext->colorspace == AVCOL_SPC_BT709) {
  114. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
  115. } else if (avccontext->colorspace == AVCOL_SPC_BT470BG) {
  116. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
  117. }
  118. if (avccontext->color_trc == AVCOL_TRC_BT709) {
  119. p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
  120. }
  121. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  122. &p_schro_params->frame_format) == -1) {
  123. av_log(avccontext, AV_LOG_ERROR,
  124. "This codec currently supports only planar YUV 4:2:0, 4:2:2"
  125. " and 4:4:4 formats.\n");
  126. return -1;
  127. }
  128. p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
  129. p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
  130. p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
  131. avccontext->width,
  132. avccontext->height);
  133. avccontext->coded_frame = &p_schro_params->picture;
  134. if (!avccontext->gop_size) {
  135. schro_encoder_setting_set_double(p_schro_params->encoder,
  136. "gop_structure",
  137. SCHRO_ENCODER_GOP_INTRA_ONLY);
  138. if (avccontext->coder_type == FF_CODER_TYPE_VLC)
  139. schro_encoder_setting_set_double(p_schro_params->encoder,
  140. "enable_noarith", 1);
  141. } else {
  142. schro_encoder_setting_set_double(p_schro_params->encoder,
  143. "au_distance", avccontext->gop_size);
  144. avccontext->has_b_frames = 1;
  145. p_schro_params->dts = -1;
  146. }
  147. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  148. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  149. if (!avccontext->global_quality) {
  150. /* lossless coding */
  151. schro_encoder_setting_set_double(p_schro_params->encoder,
  152. "rate_control",
  153. SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
  154. } else {
  155. int quality;
  156. schro_encoder_setting_set_double(p_schro_params->encoder,
  157. "rate_control",
  158. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
  159. quality = avccontext->global_quality / FF_QP2LAMBDA;
  160. if (quality > 10)
  161. quality = 10;
  162. schro_encoder_setting_set_double(p_schro_params->encoder,
  163. "quality", quality);
  164. }
  165. } else {
  166. schro_encoder_setting_set_double(p_schro_params->encoder,
  167. "rate_control",
  168. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  169. schro_encoder_setting_set_double(p_schro_params->encoder,
  170. "bitrate",
  171. avccontext->bit_rate);
  172. }
  173. if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
  174. /* All material can be coded as interlaced or progressive
  175. irrespective of the type of source material. */
  176. schro_encoder_setting_set_double(p_schro_params->encoder,
  177. "interlaced_coding", 1);
  178. schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
  179. !(avccontext->flags & CODEC_FLAG_CLOSED_GOP));
  180. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  181. * and libdirac support other bit-depth data. */
  182. schro_video_format_set_std_signal_range(p_schro_params->format,
  183. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  184. /* Set the encoder format. */
  185. schro_encoder_set_video_format(p_schro_params->encoder,
  186. p_schro_params->format);
  187. /* Set the debug level. */
  188. schro_debug_set_level(avccontext->debug);
  189. schro_encoder_start(p_schro_params->encoder);
  190. /* Initialize the encoded frame queue. */
  191. ff_schro_queue_init(&p_schro_params->enc_frame_queue);
  192. return 0;
  193. }
  194. static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
  195. const AVFrame *frame)
  196. {
  197. SchroEncoderParams *p_schro_params = avccontext->priv_data;
  198. SchroFrame *in_frame;
  199. /* Input line size may differ from what the codec supports. Especially
  200. * when transcoding from one format to another. So use avpicture_layout
  201. * to copy the frame. */
  202. in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
  203. if (in_frame)
  204. avpicture_layout((const AVPicture *)frame, avccontext->pix_fmt,
  205. avccontext->width, avccontext->height,
  206. in_frame->components[0].data,
  207. p_schro_params->frame_size);
  208. return in_frame;
  209. }
  210. static void libschroedinger_free_frame(void *data)
  211. {
  212. FFSchroEncodedFrame *enc_frame = data;
  213. av_freep(&enc_frame->p_encbuf);
  214. av_free(enc_frame);
  215. }
  216. static int libschroedinger_encode_frame(AVCodecContext *avccontext, AVPacket *pkt,
  217. const AVFrame *frame, int *got_packet)
  218. {
  219. int enc_size = 0;
  220. SchroEncoderParams *p_schro_params = avccontext->priv_data;
  221. SchroEncoder *encoder = p_schro_params->encoder;
  222. struct FFSchroEncodedFrame *p_frame_output = NULL;
  223. int go = 1;
  224. SchroBuffer *enc_buf;
  225. int presentation_frame;
  226. int parse_code;
  227. int last_frame_in_sequence = 0;
  228. int pkt_size, ret;
  229. if (!frame) {
  230. /* Push end of sequence if not already signalled. */
  231. if (!p_schro_params->eos_signalled) {
  232. schro_encoder_end_of_stream(encoder);
  233. p_schro_params->eos_signalled = 1;
  234. }
  235. } else {
  236. /* Allocate frame data to schro input buffer. */
  237. SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
  238. frame);
  239. /* Load next frame. */
  240. schro_encoder_push_frame(encoder, in_frame);
  241. }
  242. if (p_schro_params->eos_pulled)
  243. go = 0;
  244. /* Now check to see if we have any output from the encoder. */
  245. while (go) {
  246. SchroStateEnum state;
  247. state = schro_encoder_wait(encoder);
  248. switch (state) {
  249. case SCHRO_STATE_HAVE_BUFFER:
  250. case SCHRO_STATE_END_OF_STREAM:
  251. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  252. assert(enc_buf->length > 0);
  253. assert(enc_buf->length <= buf_size);
  254. parse_code = enc_buf->data[4];
  255. /* All non-frame data is prepended to actual frame data to
  256. * be able to set the pts correctly. So we don't write data
  257. * to the frame output queue until we actually have a frame
  258. */
  259. p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
  260. p_schro_params->enc_buf_size + enc_buf->length);
  261. memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
  262. enc_buf->data, enc_buf->length);
  263. p_schro_params->enc_buf_size += enc_buf->length;
  264. if (state == SCHRO_STATE_END_OF_STREAM) {
  265. p_schro_params->eos_pulled = 1;
  266. go = 0;
  267. }
  268. if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
  269. schro_buffer_unref(enc_buf);
  270. break;
  271. }
  272. /* Create output frame. */
  273. p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame));
  274. /* Set output data. */
  275. p_frame_output->size = p_schro_params->enc_buf_size;
  276. p_frame_output->p_encbuf = p_schro_params->enc_buf;
  277. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  278. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
  279. p_frame_output->key_frame = 1;
  280. /* Parse the coded frame number from the bitstream. Bytes 14
  281. * through 17 represesent the frame number. */
  282. p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
  283. ff_schro_queue_push_back(&p_schro_params->enc_frame_queue,
  284. p_frame_output);
  285. p_schro_params->enc_buf_size = 0;
  286. p_schro_params->enc_buf = NULL;
  287. schro_buffer_unref(enc_buf);
  288. break;
  289. case SCHRO_STATE_NEED_FRAME:
  290. go = 0;
  291. break;
  292. case SCHRO_STATE_AGAIN:
  293. break;
  294. default:
  295. av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
  296. return -1;
  297. }
  298. }
  299. /* Copy 'next' frame in queue. */
  300. if (p_schro_params->enc_frame_queue.size == 1 &&
  301. p_schro_params->eos_pulled)
  302. last_frame_in_sequence = 1;
  303. p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue);
  304. if (!p_frame_output)
  305. return 0;
  306. pkt_size = p_frame_output->size;
  307. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
  308. pkt_size += p_schro_params->enc_buf_size;
  309. if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
  310. av_log(avccontext, AV_LOG_ERROR, "Error getting output packet of size %d.\n", pkt_size);
  311. goto error;
  312. }
  313. memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
  314. avccontext->coded_frame->key_frame = p_frame_output->key_frame;
  315. /* Use the frame number of the encoded frame as the pts. It is OK to
  316. * do so since Dirac is a constant frame rate codec. It expects input
  317. * to be of constant frame rate. */
  318. pkt->pts =
  319. avccontext->coded_frame->pts = p_frame_output->frame_num;
  320. pkt->dts = p_schro_params->dts++;
  321. enc_size = p_frame_output->size;
  322. /* Append the end of sequence information to the last frame in the
  323. * sequence. */
  324. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
  325. memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
  326. p_schro_params->enc_buf_size);
  327. enc_size += p_schro_params->enc_buf_size;
  328. av_freep(&p_schro_params->enc_buf);
  329. p_schro_params->enc_buf_size = 0;
  330. }
  331. if (p_frame_output->key_frame)
  332. pkt->flags |= AV_PKT_FLAG_KEY;
  333. *got_packet = 1;
  334. error:
  335. /* free frame */
  336. libschroedinger_free_frame(p_frame_output);
  337. return ret;
  338. }
  339. static int libschroedinger_encode_close(AVCodecContext *avccontext)
  340. {
  341. SchroEncoderParams *p_schro_params = avccontext->priv_data;
  342. /* Close the encoder. */
  343. schro_encoder_free(p_schro_params->encoder);
  344. /* Free data in the output frame queue. */
  345. ff_schro_queue_free(&p_schro_params->enc_frame_queue,
  346. libschroedinger_free_frame);
  347. /* Free the encoder buffer. */
  348. if (p_schro_params->enc_buf_size)
  349. av_freep(&p_schro_params->enc_buf);
  350. /* Free the video format structure. */
  351. av_freep(&p_schro_params->format);
  352. return 0;
  353. }
  354. AVCodec ff_libschroedinger_encoder = {
  355. .name = "libschroedinger",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. .id = CODEC_ID_DIRAC,
  358. .priv_data_size = sizeof(SchroEncoderParams),
  359. .init = libschroedinger_encode_init,
  360. .encode2 = libschroedinger_encode_frame,
  361. .close = libschroedinger_encode_close,
  362. .capabilities = CODEC_CAP_DELAY,
  363. .pix_fmts = (const enum PixelFormat[]){
  364. PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE
  365. },
  366. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  367. };