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.

437 lines
15KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "libdirac_libschro.h"
  35. #include "libschroedinger.h"
  36. #include "bytestream.h"
  37. /** libschroedinger encoder private data */
  38. typedef struct FfmpegSchroEncoderParams {
  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. FfmpegDiracSchroQueue enc_frame_queue;
  55. /** end of sequence signalled */
  56. int eos_signalled;
  57. /** end of sequence pulled */
  58. int eos_pulled;
  59. } FfmpegSchroEncoderParams;
  60. /**
  61. * Works out Schro-compatible chroma format.
  62. */
  63. static int SetSchroChromaFormat(AVCodecContext *avccontext)
  64. {
  65. int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
  66. sizeof(ffmpeg_schro_pixel_format_map[0]);
  67. int idx;
  68. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  69. for (idx = 0; idx < num_formats; ++idx) {
  70. if (ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt ==
  71. avccontext->pix_fmt) {
  72. p_schro_params->format->chroma_format =
  73. ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt;
  74. return 0;
  75. }
  76. }
  77. av_log(avccontext, AV_LOG_ERROR,
  78. "This codec currently only supports planar YUV 4:2:0, 4:2:2"
  79. " and 4:4:4 formats.\n");
  80. return -1;
  81. }
  82. static int libschroedinger_encode_init(AVCodecContext *avccontext)
  83. {
  84. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  85. SchroVideoFormatEnum preset;
  86. /* Initialize the libraries that libschroedinger depends on. */
  87. schro_init();
  88. /* Create an encoder object. */
  89. p_schro_params->encoder = schro_encoder_new();
  90. if (!p_schro_params->encoder) {
  91. av_log(avccontext, AV_LOG_ERROR,
  92. "Unrecoverable Error: schro_encoder_new failed. ");
  93. return -1;
  94. }
  95. /* Initialize the format. */
  96. preset = ff_get_schro_video_format_preset(avccontext);
  97. p_schro_params->format =
  98. schro_encoder_get_video_format(p_schro_params->encoder);
  99. schro_video_format_set_std_video_format(p_schro_params->format, preset);
  100. p_schro_params->format->width = avccontext->width;
  101. p_schro_params->format->height = avccontext->height;
  102. if (SetSchroChromaFormat(avccontext) == -1)
  103. return -1;
  104. if (avccontext->color_primaries == AVCOL_PRI_BT709) {
  105. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_HDTV;
  106. } else if (avccontext->color_primaries == AVCOL_PRI_BT470BG) {
  107. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_625;
  108. } else if (avccontext->color_primaries == AVCOL_PRI_SMPTE170M) {
  109. p_schro_params->format->colour_primaries = SCHRO_COLOUR_PRIMARY_SDTV_525;
  110. }
  111. if (avccontext->colorspace == AVCOL_SPC_BT709) {
  112. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_HDTV;
  113. } else if (avccontext->colorspace == AVCOL_SPC_BT470BG) {
  114. p_schro_params->format->colour_matrix = SCHRO_COLOUR_MATRIX_SDTV;
  115. }
  116. if (avccontext->color_trc == AVCOL_TRC_BT709) {
  117. p_schro_params->format->transfer_function = SCHRO_TRANSFER_CHAR_TV_GAMMA;
  118. }
  119. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  120. &p_schro_params->frame_format) == -1) {
  121. av_log(avccontext, AV_LOG_ERROR,
  122. "This codec currently supports only planar YUV 4:2:0, 4:2:2"
  123. " and 4:4:4 formats.\n");
  124. return -1;
  125. }
  126. p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
  127. p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
  128. p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
  129. avccontext->width,
  130. avccontext->height);
  131. avccontext->coded_frame = &p_schro_params->picture;
  132. if (!avccontext->gop_size) {
  133. schro_encoder_setting_set_double(p_schro_params->encoder,
  134. "gop_structure",
  135. SCHRO_ENCODER_GOP_INTRA_ONLY);
  136. if (avccontext->coder_type == FF_CODER_TYPE_VLC)
  137. schro_encoder_setting_set_double(p_schro_params->encoder,
  138. "enable_noarith", 1);
  139. } else {
  140. schro_encoder_setting_set_double(p_schro_params->encoder,
  141. "au_distance", avccontext->gop_size);
  142. avccontext->has_b_frames = 1;
  143. }
  144. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  145. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  146. if (!avccontext->global_quality) {
  147. /* lossless coding */
  148. schro_encoder_setting_set_double(p_schro_params->encoder,
  149. "rate_control",
  150. SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
  151. } else {
  152. int quality;
  153. schro_encoder_setting_set_double(p_schro_params->encoder,
  154. "rate_control",
  155. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
  156. quality = avccontext->global_quality / FF_QP2LAMBDA;
  157. if (quality > 10)
  158. quality = 10;
  159. schro_encoder_setting_set_double(p_schro_params->encoder,
  160. "quality", quality);
  161. }
  162. } else {
  163. schro_encoder_setting_set_double(p_schro_params->encoder,
  164. "rate_control",
  165. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  166. schro_encoder_setting_set_double(p_schro_params->encoder,
  167. "bitrate",
  168. avccontext->bit_rate);
  169. }
  170. if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
  171. /* All material can be coded as interlaced or progressive
  172. irrespective of the type of source material. */
  173. schro_encoder_setting_set_double(p_schro_params->encoder,
  174. "interlaced_coding", 1);
  175. schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
  176. !(avccontext->flags & CODEC_FLAG_CLOSED_GOP));
  177. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  178. * and libdirac support other bit-depth data. */
  179. schro_video_format_set_std_signal_range(p_schro_params->format,
  180. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  181. /* Set the encoder format. */
  182. schro_encoder_set_video_format(p_schro_params->encoder,
  183. p_schro_params->format);
  184. /* Set the debug level. */
  185. schro_debug_set_level(avccontext->debug);
  186. schro_encoder_start(p_schro_params->encoder);
  187. /* Initialize the encoded frame queue. */
  188. ff_dirac_schro_queue_init(&p_schro_params->enc_frame_queue);
  189. return 0;
  190. }
  191. static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
  192. void *in_data)
  193. {
  194. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  195. SchroFrame *in_frame;
  196. /* Input line size may differ from what the codec supports. Especially
  197. * when transcoding from one format to another. So use avpicture_layout
  198. * to copy the frame. */
  199. in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
  200. if (in_frame)
  201. avpicture_layout((AVPicture *)in_data, avccontext->pix_fmt,
  202. avccontext->width, avccontext->height,
  203. in_frame->components[0].data,
  204. p_schro_params->frame_size);
  205. return in_frame;
  206. }
  207. static void SchroedingerFreeFrame(void *data)
  208. {
  209. FfmpegDiracSchroEncodedFrame *enc_frame = data;
  210. av_freep(&(enc_frame->p_encbuf));
  211. av_free(enc_frame);
  212. }
  213. static int libschroedinger_encode_frame(AVCodecContext *avccontext,
  214. unsigned char *frame,
  215. int buf_size, void *data)
  216. {
  217. int enc_size = 0;
  218. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  219. SchroEncoder *encoder = p_schro_params->encoder;
  220. struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
  221. int go = 1;
  222. SchroBuffer *enc_buf;
  223. int presentation_frame;
  224. int parse_code;
  225. int last_frame_in_sequence = 0;
  226. if (!data) {
  227. /* Push end of sequence if not already signalled. */
  228. if (!p_schro_params->eos_signalled) {
  229. schro_encoder_end_of_stream(encoder);
  230. p_schro_params->eos_signalled = 1;
  231. }
  232. } else {
  233. /* Allocate frame data to schro input buffer. */
  234. SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
  235. data);
  236. /* Load next frame. */
  237. schro_encoder_push_frame(encoder, in_frame);
  238. }
  239. if (p_schro_params->eos_pulled)
  240. go = 0;
  241. /* Now check to see if we have any output from the encoder. */
  242. while (go) {
  243. SchroStateEnum state;
  244. state = schro_encoder_wait(encoder);
  245. switch (state) {
  246. case SCHRO_STATE_HAVE_BUFFER:
  247. case SCHRO_STATE_END_OF_STREAM:
  248. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  249. assert(enc_buf->length > 0);
  250. assert(enc_buf->length <= buf_size);
  251. parse_code = enc_buf->data[4];
  252. /* All non-frame data is prepended to actual frame data to
  253. * be able to set the pts correctly. So we don't write data
  254. * to the frame output queue until we actually have a frame
  255. */
  256. p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
  257. p_schro_params->enc_buf_size + enc_buf->length);
  258. memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
  259. enc_buf->data, enc_buf->length);
  260. p_schro_params->enc_buf_size += enc_buf->length;
  261. if (state == SCHRO_STATE_END_OF_STREAM) {
  262. p_schro_params->eos_pulled = 1;
  263. go = 0;
  264. }
  265. if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
  266. schro_buffer_unref(enc_buf);
  267. break;
  268. }
  269. /* Create output frame. */
  270. p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
  271. /* Set output data. */
  272. p_frame_output->size = p_schro_params->enc_buf_size;
  273. p_frame_output->p_encbuf = p_schro_params->enc_buf;
  274. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  275. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
  276. p_frame_output->key_frame = 1;
  277. /* Parse the coded frame number from the bitstream. Bytes 14
  278. * through 17 represesent the frame number. */
  279. p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
  280. ff_dirac_schro_queue_push_back(&p_schro_params->enc_frame_queue,
  281. p_frame_output);
  282. p_schro_params->enc_buf_size = 0;
  283. p_schro_params->enc_buf = NULL;
  284. schro_buffer_unref(enc_buf);
  285. break;
  286. case SCHRO_STATE_NEED_FRAME:
  287. go = 0;
  288. break;
  289. case SCHRO_STATE_AGAIN:
  290. break;
  291. default:
  292. av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
  293. return -1;
  294. }
  295. }
  296. /* Copy 'next' frame in queue. */
  297. if (p_schro_params->enc_frame_queue.size == 1 &&
  298. p_schro_params->eos_pulled)
  299. last_frame_in_sequence = 1;
  300. p_frame_output = ff_dirac_schro_queue_pop(&p_schro_params->enc_frame_queue);
  301. if (!p_frame_output)
  302. return 0;
  303. memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size);
  304. avccontext->coded_frame->key_frame = p_frame_output->key_frame;
  305. /* Use the frame number of the encoded frame as the pts. It is OK to
  306. * do so since Dirac is a constant frame rate codec. It expects input
  307. * to be of constant frame rate. */
  308. avccontext->coded_frame->pts = p_frame_output->frame_num;
  309. enc_size = p_frame_output->size;
  310. /* Append the end of sequence information to the last frame in the
  311. * sequence. */
  312. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
  313. memcpy(frame + enc_size, p_schro_params->enc_buf,
  314. p_schro_params->enc_buf_size);
  315. enc_size += p_schro_params->enc_buf_size;
  316. av_freep(&p_schro_params->enc_buf);
  317. p_schro_params->enc_buf_size = 0;
  318. }
  319. /* free frame */
  320. SchroedingerFreeFrame(p_frame_output);
  321. return enc_size;
  322. }
  323. static int libschroedinger_encode_close(AVCodecContext *avccontext)
  324. {
  325. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  326. /* Close the encoder. */
  327. schro_encoder_free(p_schro_params->encoder);
  328. /* Free data in the output frame queue. */
  329. ff_dirac_schro_queue_free(&p_schro_params->enc_frame_queue,
  330. SchroedingerFreeFrame);
  331. /* Free the encoder buffer. */
  332. if (p_schro_params->enc_buf_size)
  333. av_freep(&p_schro_params->enc_buf);
  334. /* Free the video format structure. */
  335. av_freep(&p_schro_params->format);
  336. return 0;
  337. }
  338. AVCodec libschroedinger_encoder = {
  339. "libschroedinger",
  340. AVMEDIA_TYPE_VIDEO,
  341. CODEC_ID_DIRAC,
  342. sizeof(FfmpegSchroEncoderParams),
  343. libschroedinger_encode_init,
  344. libschroedinger_encode_frame,
  345. libschroedinger_encode_close,
  346. .capabilities = CODEC_CAP_DELAY,
  347. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
  348. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  349. };