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.

419 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. #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 (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  105. &p_schro_params->frame_format) == -1) {
  106. av_log(avccontext, AV_LOG_ERROR,
  107. "This codec currently supports only planar YUV 4:2:0, 4:2:2"
  108. " and 4:4:4 formats.\n");
  109. return -1;
  110. }
  111. p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
  112. p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
  113. p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
  114. avccontext->width,
  115. avccontext->height);
  116. avccontext->coded_frame = &p_schro_params->picture;
  117. if (!avccontext->gop_size) {
  118. schro_encoder_setting_set_double(p_schro_params->encoder,
  119. "gop_structure",
  120. SCHRO_ENCODER_GOP_INTRA_ONLY);
  121. if (avccontext->coder_type == FF_CODER_TYPE_VLC)
  122. schro_encoder_setting_set_double(p_schro_params->encoder,
  123. "enable_noarith", 1);
  124. } else {
  125. schro_encoder_setting_set_double(p_schro_params->encoder,
  126. "au_distance", avccontext->gop_size);
  127. avccontext->has_b_frames = 1;
  128. }
  129. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  130. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  131. if (!avccontext->global_quality) {
  132. /* lossless coding */
  133. schro_encoder_setting_set_double(p_schro_params->encoder,
  134. "rate_control",
  135. SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
  136. } else {
  137. int quality;
  138. schro_encoder_setting_set_double(p_schro_params->encoder,
  139. "rate_control",
  140. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY);
  141. quality = avccontext->global_quality / FF_QP2LAMBDA;
  142. if (quality > 10)
  143. quality = 10;
  144. schro_encoder_setting_set_double(p_schro_params->encoder,
  145. "quality", quality);
  146. }
  147. } else {
  148. schro_encoder_setting_set_double(p_schro_params->encoder,
  149. "rate_control",
  150. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  151. schro_encoder_setting_set_double(p_schro_params->encoder,
  152. "bitrate",
  153. avccontext->bit_rate);
  154. }
  155. if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
  156. /* All material can be coded as interlaced or progressive
  157. irrespective of the type of source material. */
  158. schro_encoder_setting_set_double(p_schro_params->encoder,
  159. "interlaced_coding", 1);
  160. schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop",
  161. !(avccontext->flags & CODEC_FLAG_CLOSED_GOP));
  162. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  163. * and libdirac support other bit-depth data. */
  164. schro_video_format_set_std_signal_range(p_schro_params->format,
  165. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  166. /* Set the encoder format. */
  167. schro_encoder_set_video_format(p_schro_params->encoder,
  168. p_schro_params->format);
  169. /* Set the debug level. */
  170. schro_debug_set_level(avccontext->debug);
  171. schro_encoder_start(p_schro_params->encoder);
  172. /* Initialize the encoded frame queue. */
  173. ff_dirac_schro_queue_init(&p_schro_params->enc_frame_queue);
  174. return 0;
  175. }
  176. static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
  177. void *in_data)
  178. {
  179. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  180. SchroFrame *in_frame;
  181. /* Input line size may differ from what the codec supports. Especially
  182. * when transcoding from one format to another. So use avpicture_layout
  183. * to copy the frame. */
  184. in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
  185. if (in_frame)
  186. avpicture_layout((AVPicture *)in_data, avccontext->pix_fmt,
  187. avccontext->width, avccontext->height,
  188. in_frame->components[0].data,
  189. p_schro_params->frame_size);
  190. return in_frame;
  191. }
  192. static void SchroedingerFreeFrame(void *data)
  193. {
  194. FfmpegDiracSchroEncodedFrame *enc_frame = data;
  195. av_freep(&(enc_frame->p_encbuf));
  196. av_free(enc_frame);
  197. }
  198. static int libschroedinger_encode_frame(AVCodecContext *avccontext,
  199. unsigned char *frame,
  200. int buf_size, void *data)
  201. {
  202. int enc_size = 0;
  203. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  204. SchroEncoder *encoder = p_schro_params->encoder;
  205. struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
  206. int go = 1;
  207. SchroBuffer *enc_buf;
  208. int presentation_frame;
  209. int parse_code;
  210. int last_frame_in_sequence = 0;
  211. if (!data) {
  212. /* Push end of sequence if not already signalled. */
  213. if (!p_schro_params->eos_signalled) {
  214. schro_encoder_end_of_stream(encoder);
  215. p_schro_params->eos_signalled = 1;
  216. }
  217. } else {
  218. /* Allocate frame data to schro input buffer. */
  219. SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
  220. data);
  221. /* Load next frame. */
  222. schro_encoder_push_frame(encoder, in_frame);
  223. }
  224. if (p_schro_params->eos_pulled)
  225. go = 0;
  226. /* Now check to see if we have any output from the encoder. */
  227. while (go) {
  228. SchroStateEnum state;
  229. state = schro_encoder_wait(encoder);
  230. switch (state) {
  231. case SCHRO_STATE_HAVE_BUFFER:
  232. case SCHRO_STATE_END_OF_STREAM:
  233. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  234. assert(enc_buf->length > 0);
  235. assert(enc_buf->length <= buf_size);
  236. parse_code = enc_buf->data[4];
  237. /* All non-frame data is prepended to actual frame data to
  238. * be able to set the pts correctly. So we don't write data
  239. * to the frame output queue until we actually have a frame
  240. */
  241. p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
  242. p_schro_params->enc_buf_size + enc_buf->length);
  243. memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
  244. enc_buf->data, enc_buf->length);
  245. p_schro_params->enc_buf_size += enc_buf->length;
  246. if (state == SCHRO_STATE_END_OF_STREAM) {
  247. p_schro_params->eos_pulled = 1;
  248. go = 0;
  249. }
  250. if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
  251. schro_buffer_unref(enc_buf);
  252. break;
  253. }
  254. /* Create output frame. */
  255. p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
  256. /* Set output data. */
  257. p_frame_output->size = p_schro_params->enc_buf_size;
  258. p_frame_output->p_encbuf = p_schro_params->enc_buf;
  259. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  260. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
  261. p_frame_output->key_frame = 1;
  262. /* Parse the coded frame number from the bitstream. Bytes 14
  263. * through 17 represesent the frame number. */
  264. p_frame_output->frame_num = AV_RB32(enc_buf->data + 13);
  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. };