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.

389 lines
13KB

  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 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. {
  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. /** queue storing encoded frames */
  50. FfmpegDiracSchroQueue enc_frame_queue;
  51. /** end of sequence signalled */
  52. int eos_signalled;
  53. /** end of sequence pulled */
  54. int eos_pulled;
  55. } FfmpegSchroEncoderParams;
  56. /**
  57. * Works out Schro-compatible chroma format.
  58. */
  59. static int SetSchroChromaFormat(AVCodecContext *avccontext)
  60. {
  61. int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
  62. sizeof(ffmpeg_schro_pixel_format_map[0]);
  63. int idx;
  64. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  65. for (idx = 0; idx < num_formats; ++idx) {
  66. if (ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt ==
  67. avccontext->pix_fmt) {
  68. p_schro_params->format->chroma_format =
  69. ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt;
  70. return 0;
  71. }
  72. }
  73. av_log (avccontext, AV_LOG_ERROR,
  74. "This codec currently only supports planar YUV 4:2:0, 4:2:2"
  75. " and 4:4:4 formats.\n");
  76. return -1;
  77. }
  78. static int libschroedinger_encode_init(AVCodecContext *avccontext)
  79. {
  80. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  81. SchroVideoFormatEnum preset;
  82. /* Initialize the libraries that libschroedinger depends on. */
  83. schro_init();
  84. /* Create an encoder object. */
  85. p_schro_params->encoder = schro_encoder_new();
  86. if (!p_schro_params->encoder) {
  87. av_log(avccontext, AV_LOG_ERROR,
  88. "Unrecoverable Error: schro_encoder_new failed. ");
  89. return -1;
  90. }
  91. /* Initialize the format. */
  92. preset = ff_get_schro_video_format_preset(avccontext);
  93. p_schro_params->format =
  94. schro_encoder_get_video_format(p_schro_params->encoder);
  95. schro_video_format_set_std_video_format (p_schro_params->format, preset);
  96. p_schro_params->format->width = avccontext->width;
  97. p_schro_params->format->height = avccontext->height;
  98. if (SetSchroChromaFormat(avccontext) == -1)
  99. return -1;
  100. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  101. &p_schro_params->frame_format) == -1) {
  102. av_log (avccontext, AV_LOG_ERROR,
  103. "This codec currently supports only planar YUV 4:2:0, 4:2:2"
  104. " and 4:4:4 formats.\n");
  105. return -1;
  106. }
  107. p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
  108. p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
  109. p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
  110. avccontext->width,
  111. avccontext->height);
  112. avccontext->coded_frame = &p_schro_params->picture;
  113. if (avccontext->gop_size == 0){
  114. schro_encoder_setting_set_double (p_schro_params->encoder,
  115. "gop_structure",
  116. SCHRO_ENCODER_GOP_INTRA_ONLY);
  117. }
  118. else {
  119. schro_encoder_setting_set_double (p_schro_params->encoder,
  120. "gop_structure",
  121. SCHRO_ENCODER_GOP_BIREF);
  122. avccontext->has_b_frames = 1;
  123. }
  124. /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
  125. if (avccontext->flags & CODEC_FLAG_QSCALE) {
  126. if (avccontext->global_quality == 0) {
  127. /* lossless coding */
  128. schro_encoder_setting_set_double (p_schro_params->encoder,
  129. "rate_control",
  130. SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
  131. } else {
  132. int noise_threshold;
  133. schro_encoder_setting_set_double (p_schro_params->encoder,
  134. "rate_control",
  135. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD);
  136. noise_threshold = avccontext->global_quality/FF_QP2LAMBDA;
  137. if (noise_threshold > 100)
  138. noise_threshold = 100;
  139. schro_encoder_setting_set_double (p_schro_params->encoder,
  140. "noise_threshold",
  141. noise_threshold);
  142. }
  143. }
  144. else {
  145. schro_encoder_setting_set_double ( p_schro_params->encoder,
  146. "rate_control",
  147. SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
  148. schro_encoder_setting_set_double (p_schro_params->encoder,
  149. "bitrate",
  150. avccontext->bit_rate);
  151. }
  152. if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) {
  153. /* All material can be coded as interlaced or progressive
  154. irrespective of the type of source material. */
  155. schro_encoder_setting_set_double (p_schro_params->encoder,
  156. "interlaced_coding", 1);
  157. }
  158. /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
  159. * and libdirac support other bit-depth data. */
  160. schro_video_format_set_std_signal_range(p_schro_params->format,
  161. SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
  162. /* Hardcode motion vector precision to quarter pixel. */
  163. schro_encoder_setting_set_double (p_schro_params->encoder,
  164. "mv_precision", 2);
  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 = schro_frame_new_and_alloc (NULL,
  184. p_schro_params->frame_format,
  185. p_schro_params->format->width,
  186. p_schro_params->format->height);
  187. avpicture_layout ((AVPicture *)in_data, avccontext->pix_fmt,
  188. avccontext->width, avccontext->height,
  189. in_frame->components[0].data,
  190. p_schro_params->frame_size);
  191. return in_frame;
  192. }
  193. static void SchroedingerFreeFrame(void *data)
  194. {
  195. FfmpegDiracSchroEncodedFrame *enc_frame = data;
  196. av_freep (&(enc_frame->p_encbuf));
  197. av_free(enc_frame);
  198. }
  199. static int libschroedinger_encode_frame(AVCodecContext *avccontext,
  200. unsigned char *frame,
  201. int buf_size, void *data)
  202. {
  203. int enc_size = 0;
  204. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  205. SchroEncoder *encoder = p_schro_params->encoder;
  206. struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
  207. int go = 1;
  208. SchroBuffer *enc_buf;
  209. int presentation_frame;
  210. int parse_code;
  211. if(data == NULL) {
  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. {
  232. case SCHRO_STATE_HAVE_BUFFER:
  233. case SCHRO_STATE_END_OF_STREAM:
  234. enc_buf = schro_encoder_pull (encoder,
  235. &presentation_frame);
  236. assert (enc_buf->length > 0);
  237. assert (enc_buf->length <= buf_size);
  238. /* Create output frame. */
  239. p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
  240. /* Set output data. */
  241. p_frame_output->size = enc_buf->length;
  242. p_frame_output->p_encbuf = av_malloc(enc_buf->length);
  243. memcpy(p_frame_output->p_encbuf, enc_buf->data, enc_buf->length);
  244. parse_code = enc_buf->data[4];
  245. if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
  246. SCHRO_PARSE_CODE_IS_REFERENCE(parse_code)) {
  247. p_frame_output->key_frame = 1;
  248. }
  249. /* Parse the coded frame number from the bitstream. Bytes 14
  250. * through 17 represesent the frame number. */
  251. if (SCHRO_PARSE_CODE_IS_PICTURE(parse_code))
  252. {
  253. assert (enc_buf->length >= 17);
  254. p_frame_output->frame_num = (enc_buf->data[13] << 24) +
  255. (enc_buf->data[14] << 16) +
  256. (enc_buf->data[15] << 8) +
  257. enc_buf->data[16];
  258. }
  259. ff_dirac_schro_queue_push_back (&p_schro_params->enc_frame_queue,
  260. p_frame_output);
  261. schro_buffer_unref (enc_buf);
  262. if (state == SCHRO_STATE_END_OF_STREAM) {
  263. p_schro_params->eos_pulled = 1;
  264. go = 0;
  265. }
  266. break;
  267. case SCHRO_STATE_NEED_FRAME:
  268. go = 0;
  269. break;
  270. case SCHRO_STATE_AGAIN:
  271. break;
  272. default:
  273. av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
  274. return -1;
  275. }
  276. }
  277. /* Copy 'next' frame in queue. */
  278. p_frame_output =
  279. ff_dirac_schro_queue_pop (&p_schro_params->enc_frame_queue);
  280. if (p_frame_output == NULL)
  281. return 0;
  282. memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size);
  283. avccontext->coded_frame->key_frame = p_frame_output->key_frame;
  284. /* Use the frame number of the encoded frame as the pts. It is OK to
  285. * do so since Dirac is a constant frame rate codec. It expects input
  286. * to be of constant frame rate. */
  287. avccontext->coded_frame->pts = p_frame_output->frame_num;
  288. enc_size = p_frame_output->size;
  289. /* free frame */
  290. SchroedingerFreeFrame (p_frame_output);
  291. return enc_size;
  292. }
  293. static int libschroedinger_encode_close(AVCodecContext *avccontext)
  294. {
  295. FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
  296. /* Close the encoder. */
  297. schro_encoder_free(p_schro_params->encoder);
  298. /* Free data in the output frame queue. */
  299. ff_dirac_schro_queue_free (&p_schro_params->enc_frame_queue,
  300. SchroedingerFreeFrame);
  301. /* Free the video format structure. */
  302. av_freep(&p_schro_params->format);
  303. return 0 ;
  304. }
  305. AVCodec libschroedinger_encoder = {
  306. "libschroedinger",
  307. CODEC_TYPE_VIDEO,
  308. CODEC_ID_DIRAC,
  309. sizeof(FfmpegSchroEncoderParams),
  310. libschroedinger_encode_init,
  311. libschroedinger_encode_frame,
  312. libschroedinger_encode_close,
  313. .capabilities= CODEC_CAP_DELAY,
  314. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
  315. .long_name= NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  316. };