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.

418 lines
14KB

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