libschroedingerenc.c 15 KB

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