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.

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