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.

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