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.

420 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 libavcodec/libschroedingerenc.c
  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. "gop_structure",
  126. SCHRO_ENCODER_GOP_BIREF);
  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 noise_threshold;
  138. schro_encoder_setting_set_double(p_schro_params->encoder,
  139. "rate_control",
  140. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD);
  141. noise_threshold = avccontext->global_quality / FF_QP2LAMBDA;
  142. if (noise_threshold > 100)
  143. noise_threshold = 100;
  144. schro_encoder_setting_set_double(p_schro_params->encoder,
  145. "noise_threshold",
  146. noise_threshold);
  147. }
  148. } else {
  149. schro_encoder_setting_set_double(p_schro_params->encoder,
  150. "rate_control",
  151. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  152. schro_encoder_setting_set_double(p_schro_params->encoder,
  153. "bitrate",
  154. avccontext->bit_rate);
  155. }
  156. if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
  157. /* All material can be coded as interlaced or progressive
  158. irrespective of the type of source material. */
  159. schro_encoder_setting_set_double(p_schro_params->encoder,
  160. "interlaced_coding", 1);
  161. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  162. * and libdirac support other bit-depth data. */
  163. schro_video_format_set_std_signal_range(p_schro_params->format,
  164. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  165. /* Set the encoder format. */
  166. schro_encoder_set_video_format(p_schro_params->encoder,
  167. p_schro_params->format);
  168. /* Set the debug level. */
  169. schro_debug_set_level(avccontext->debug);
  170. schro_encoder_start(p_schro_params->encoder);
  171. /* Initialize the encoded frame queue. */
  172. ff_dirac_schro_queue_init(&p_schro_params->enc_frame_queue);
  173. return 0;
  174. }
  175. static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
  176. void *in_data)
  177. {
  178. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  179. SchroFrame *in_frame;
  180. /* Input line size may differ from what the codec supports. Especially
  181. * when transcoding from one format to another. So use avpicture_layout
  182. * to copy the frame. */
  183. in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
  184. if (in_frame)
  185. avpicture_layout((AVPicture *)in_data, avccontext->pix_fmt,
  186. avccontext->width, avccontext->height,
  187. in_frame->components[0].data,
  188. p_schro_params->frame_size);
  189. return in_frame;
  190. }
  191. static void SchroedingerFreeFrame(void *data)
  192. {
  193. FfmpegDiracSchroEncodedFrame *enc_frame = data;
  194. av_freep(&(enc_frame->p_encbuf));
  195. av_free(enc_frame);
  196. }
  197. static int libschroedinger_encode_frame(AVCodecContext *avccontext,
  198. unsigned char *frame,
  199. int buf_size, void *data)
  200. {
  201. int enc_size = 0;
  202. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  203. SchroEncoder *encoder = p_schro_params->encoder;
  204. struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
  205. int go = 1;
  206. SchroBuffer *enc_buf;
  207. int presentation_frame;
  208. int parse_code;
  209. int last_frame_in_sequence = 0;
  210. if (!data) {
  211. /* Push end of sequence if not already signalled. */
  212. if (!p_schro_params->eos_signalled) {
  213. schro_encoder_end_of_stream(encoder);
  214. p_schro_params->eos_signalled = 1;
  215. }
  216. } else {
  217. /* Allocate frame data to schro input buffer. */
  218. SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
  219. data);
  220. /* Load next frame. */
  221. schro_encoder_push_frame(encoder, in_frame);
  222. }
  223. if (p_schro_params->eos_pulled)
  224. go = 0;
  225. /* Now check to see if we have any output from the encoder. */
  226. while (go) {
  227. SchroStateEnum state;
  228. state = schro_encoder_wait(encoder);
  229. switch (state) {
  230. case SCHRO_STATE_HAVE_BUFFER:
  231. case SCHRO_STATE_END_OF_STREAM:
  232. enc_buf = schro_encoder_pull(encoder, &presentation_frame);
  233. assert(enc_buf->length > 0);
  234. assert(enc_buf->length <= buf_size);
  235. parse_code = enc_buf->data[4];
  236. /* All non-frame data is prepended to actual frame data to
  237. * be able to set the pts correctly. So we don't write data
  238. * to the frame output queue until we actually have a frame
  239. */
  240. p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
  241. p_schro_params->enc_buf_size + enc_buf->length);
  242. memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
  243. enc_buf->data, enc_buf->length);
  244. p_schro_params->enc_buf_size += enc_buf->length;
  245. if (state == SCHRO_STATE_END_OF_STREAM) {
  246. p_schro_params->eos_pulled = 1;
  247. go = 0;
  248. }
  249. if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) {
  250. schro_buffer_unref(enc_buf);
  251. break;
  252. }
  253. /* Create output frame. */
  254. p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
  255. /* Set output data. */
  256. p_frame_output->size = p_schro_params->enc_buf_size;
  257. p_frame_output->p_encbuf = p_schro_params->enc_buf;
  258. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  259. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code))
  260. p_frame_output->key_frame = 1;
  261. /* Parse the coded frame number from the bitstream. Bytes 14
  262. * through 17 represesent the frame number. */
  263. p_frame_output->frame_num = (enc_buf->data[13] << 24) +
  264. (enc_buf->data[14] << 16) +
  265. (enc_buf->data[15] << 8) +
  266. enc_buf->data[16];
  267. ff_dirac_schro_queue_push_back(&p_schro_params->enc_frame_queue,
  268. p_frame_output);
  269. p_schro_params->enc_buf_size = 0;
  270. p_schro_params->enc_buf = NULL;
  271. schro_buffer_unref(enc_buf);
  272. break;
  273. case SCHRO_STATE_NEED_FRAME:
  274. go = 0;
  275. break;
  276. case SCHRO_STATE_AGAIN:
  277. break;
  278. default:
  279. av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
  280. return -1;
  281. }
  282. }
  283. /* Copy 'next' frame in queue. */
  284. if (p_schro_params->enc_frame_queue.size == 1 &&
  285. p_schro_params->eos_pulled)
  286. last_frame_in_sequence = 1;
  287. p_frame_output = ff_dirac_schro_queue_pop(&p_schro_params->enc_frame_queue);
  288. if (!p_frame_output)
  289. return 0;
  290. memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size);
  291. avccontext->coded_frame->key_frame = p_frame_output->key_frame;
  292. /* Use the frame number of the encoded frame as the pts. It is OK to
  293. * do so since Dirac is a constant frame rate codec. It expects input
  294. * to be of constant frame rate. */
  295. avccontext->coded_frame->pts = p_frame_output->frame_num;
  296. enc_size = p_frame_output->size;
  297. /* Append the end of sequence information to the last frame in the
  298. * sequence. */
  299. if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
  300. memcpy(frame + enc_size, p_schro_params->enc_buf,
  301. p_schro_params->enc_buf_size);
  302. enc_size += p_schro_params->enc_buf_size;
  303. av_freep(&p_schro_params->enc_buf);
  304. p_schro_params->enc_buf_size = 0;
  305. }
  306. /* free frame */
  307. SchroedingerFreeFrame(p_frame_output);
  308. return enc_size;
  309. }
  310. static int libschroedinger_encode_close(AVCodecContext *avccontext)
  311. {
  312. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  313. /* Close the encoder. */
  314. schro_encoder_free(p_schro_params->encoder);
  315. /* Free data in the output frame queue. */
  316. ff_dirac_schro_queue_free(&p_schro_params->enc_frame_queue,
  317. SchroedingerFreeFrame);
  318. /* Free the encoder buffer. */
  319. if (p_schro_params->enc_buf_size)
  320. av_freep(&p_schro_params->enc_buf);
  321. /* Free the video format structure. */
  322. av_freep(&p_schro_params->format);
  323. return 0;
  324. }
  325. AVCodec libschroedinger_encoder = {
  326. "libschroedinger",
  327. CODEC_TYPE_VIDEO,
  328. CODEC_ID_DIRAC,
  329. sizeof(FfmpegSchroEncoderParams),
  330. libschroedinger_encode_init,
  331. libschroedinger_encode_frame,
  332. libschroedinger_encode_close,
  333. .capabilities = CODEC_CAP_DELAY,
  334. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
  335. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  336. };