libschroedingerdec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Dirac decoder 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/libschroedingerdec.c
  23. * Dirac decoder 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. #include "avcodec.h"
  29. #include "libdirac_libschro.h"
  30. #include "libschroedinger.h"
  31. #undef NDEBUG
  32. #include <assert.h>
  33. #include <schroedinger/schro.h>
  34. #include <schroedinger/schrodebug.h>
  35. #include <schroedinger/schrovideoformat.h>
  36. /** libschroedinger decoder private data */
  37. typedef struct FfmpegSchroDecoderParams
  38. {
  39. /** Schroedinger video format */
  40. SchroVideoFormat *format;
  41. /** Schroedinger frame format */
  42. SchroFrameFormat frame_format;
  43. /** decoder handle */
  44. SchroDecoder* decoder;
  45. /** queue storing decoded frames */
  46. FfmpegDiracSchroQueue dec_frame_queue;
  47. /** end of sequence signalled */
  48. int eos_signalled;
  49. /** end of sequence pulled */
  50. int eos_pulled;
  51. /** decoded picture */
  52. AVPicture dec_pic;
  53. } FfmpegSchroDecoderParams;
  54. typedef struct FfmpegSchroParseUnitContext
  55. {
  56. const uint8_t *buf;
  57. int buf_size;
  58. } FfmpegSchroParseUnitContext;
  59. static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
  60. void *priv);
  61. static void FfmpegSchroParseContextInit (FfmpegSchroParseUnitContext *parse_ctx,
  62. const uint8_t *buf, int buf_size)
  63. {
  64. parse_ctx->buf = buf;
  65. parse_ctx->buf_size = buf_size;
  66. }
  67. static SchroBuffer* FfmpegFindNextSchroParseUnit (FfmpegSchroParseUnitContext *parse_ctx)
  68. {
  69. SchroBuffer *enc_buf = NULL;
  70. int next_pu_offset = 0;
  71. unsigned char *in_buf;
  72. if (parse_ctx->buf_size < 13 ||
  73. parse_ctx->buf[0] != 'B' ||
  74. parse_ctx->buf[1] != 'B' ||
  75. parse_ctx->buf[2] != 'C' ||
  76. parse_ctx->buf[3] != 'D')
  77. return NULL;
  78. next_pu_offset = (parse_ctx->buf[5] << 24) +
  79. (parse_ctx->buf[6] << 16) +
  80. (parse_ctx->buf[7] << 8) +
  81. parse_ctx->buf[8];
  82. if (next_pu_offset == 0 &&
  83. SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
  84. next_pu_offset = 13;
  85. if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
  86. return NULL;
  87. in_buf = av_malloc(next_pu_offset);
  88. memcpy (in_buf, parse_ctx->buf, next_pu_offset);
  89. enc_buf = schro_buffer_new_with_data (in_buf, next_pu_offset);
  90. enc_buf->free = libschroedinger_decode_buffer_free;
  91. enc_buf->priv = in_buf;
  92. parse_ctx->buf += next_pu_offset;
  93. parse_ctx->buf_size -= next_pu_offset;
  94. return enc_buf;
  95. }
  96. /**
  97. * Returns FFmpeg chroma format.
  98. */
  99. static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt)
  100. {
  101. int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
  102. sizeof(ffmpeg_schro_pixel_format_map[0]);
  103. int idx;
  104. for (idx = 0; idx < num_formats; ++idx) {
  105. if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
  106. return ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt;
  107. }
  108. }
  109. return PIX_FMT_NONE;
  110. }
  111. static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
  112. {
  113. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ;
  114. /* First of all, initialize our supporting libraries. */
  115. schro_init();
  116. schro_debug_set_level(avccontext->debug);
  117. p_schro_params->decoder = schro_decoder_new();
  118. schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
  119. if (!p_schro_params->decoder)
  120. return -1;
  121. /* Initialize the decoded frame queue. */
  122. ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
  123. return 0 ;
  124. }
  125. static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
  126. void *priv)
  127. {
  128. av_freep(&priv);
  129. }
  130. static void libschroedinger_decode_frame_free (void *frame)
  131. {
  132. schro_frame_unref(frame);
  133. }
  134. static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
  135. {
  136. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  137. SchroDecoder *decoder = p_schro_params->decoder;
  138. p_schro_params->format = schro_decoder_get_video_format (decoder);
  139. /* Tell FFmpeg about sequence details. */
  140. if(avcodec_check_dimensions(avccontext, p_schro_params->format->width,
  141. p_schro_params->format->height) < 0) {
  142. av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
  143. p_schro_params->format->width, p_schro_params->format->height);
  144. avccontext->height = avccontext->width = 0;
  145. return;
  146. }
  147. avccontext->height = p_schro_params->format->height;
  148. avccontext->width = p_schro_params->format->width;
  149. avccontext->pix_fmt =
  150. GetFfmpegChromaFormat(p_schro_params->format->chroma_format);
  151. if (ff_get_schro_frame_format( p_schro_params->format->chroma_format,
  152. &p_schro_params->frame_format) == -1) {
  153. av_log (avccontext, AV_LOG_ERROR,
  154. "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
  155. "and 4:4:4 formats.\n");
  156. return;
  157. }
  158. avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
  159. avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
  160. if (p_schro_params->dec_pic.data[0] == NULL)
  161. {
  162. avpicture_alloc(&p_schro_params->dec_pic,
  163. avccontext->pix_fmt,
  164. avccontext->width,
  165. avccontext->height);
  166. }
  167. }
  168. static int libschroedinger_decode_frame(AVCodecContext *avccontext,
  169. void *data, int *data_size,
  170. const uint8_t *buf, int buf_size)
  171. {
  172. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  173. SchroDecoder *decoder = p_schro_params->decoder;
  174. SchroVideoFormat *format;
  175. AVPicture *picture = data;
  176. SchroBuffer *enc_buf;
  177. SchroFrame* frame;
  178. int state;
  179. int go = 1;
  180. int outer = 1;
  181. FfmpegSchroParseUnitContext parse_ctx;
  182. *data_size = 0;
  183. FfmpegSchroParseContextInit (&parse_ctx, buf, buf_size);
  184. if (buf_size == 0) {
  185. if (!p_schro_params->eos_signalled) {
  186. state = schro_decoder_push_end_of_stream(decoder);
  187. p_schro_params->eos_signalled = 1;
  188. }
  189. }
  190. /* Loop through all the individual parse units in the input buffer */
  191. do {
  192. if ((enc_buf = FfmpegFindNextSchroParseUnit(&parse_ctx))) {
  193. /* Push buffer into decoder. */
  194. if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
  195. SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
  196. avccontext->has_b_frames = 1;
  197. state = schro_decoder_push (decoder, enc_buf);
  198. if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
  199. libschroedinger_handle_first_access_unit(avccontext);
  200. go = 1;
  201. }
  202. else
  203. outer = 0;
  204. format = p_schro_params->format;
  205. while (go) {
  206. /* Parse data and process result. */
  207. state = schro_decoder_wait (decoder);
  208. switch (state)
  209. {
  210. case SCHRO_DECODER_FIRST_ACCESS_UNIT:
  211. libschroedinger_handle_first_access_unit (avccontext);
  212. break;
  213. case SCHRO_DECODER_NEED_BITS:
  214. /* Need more input data - stop iterating over what we have. */
  215. go = 0;
  216. break;
  217. case SCHRO_DECODER_NEED_FRAME:
  218. /* Decoder needs a frame - create one and push it in. */
  219. frame = schro_frame_new_and_alloc(NULL,
  220. p_schro_params->frame_format,
  221. format->width,
  222. format->height);
  223. schro_decoder_add_output_picture (decoder, frame);
  224. break;
  225. case SCHRO_DECODER_OK:
  226. /* Pull a frame out of the decoder. */
  227. frame = schro_decoder_pull (decoder);
  228. if (frame) {
  229. ff_dirac_schro_queue_push_back(
  230. &p_schro_params->dec_frame_queue,
  231. frame);
  232. }
  233. break;
  234. case SCHRO_DECODER_EOS:
  235. go = 0;
  236. p_schro_params->eos_pulled = 1;
  237. schro_decoder_reset (decoder);
  238. outer = 0;
  239. break;
  240. case SCHRO_DECODER_ERROR:
  241. return -1;
  242. break;
  243. }
  244. }
  245. } while(outer);
  246. /* Grab next frame to be returned from the top of the queue. */
  247. frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
  248. if (frame != NULL) {
  249. memcpy (p_schro_params->dec_pic.data[0],
  250. frame->components[0].data,
  251. frame->components[0].length);
  252. memcpy (p_schro_params->dec_pic.data[1],
  253. frame->components[1].data,
  254. frame->components[1].length);
  255. memcpy (p_schro_params->dec_pic.data[2],
  256. frame->components[2].data,
  257. frame->components[2].length);
  258. /* Fill picture with current buffer data from Schroedinger. */
  259. avpicture_fill(picture, p_schro_params->dec_pic.data[0],
  260. avccontext->pix_fmt,
  261. avccontext->width, avccontext->height);
  262. *data_size = sizeof(AVPicture);
  263. /* Now free the frame resources. */
  264. libschroedinger_decode_frame_free (frame);
  265. }
  266. return buf_size;
  267. }
  268. static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
  269. {
  270. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  271. /* Free the decoder. */
  272. schro_decoder_free (p_schro_params->decoder);
  273. av_freep(&p_schro_params->format);
  274. avpicture_free (&p_schro_params->dec_pic);
  275. /* Free data in the output frame queue. */
  276. ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
  277. libschroedinger_decode_frame_free);
  278. return 0 ;
  279. }
  280. static void libschroedinger_flush (AVCodecContext *avccontext)
  281. {
  282. /* Got a seek request. Free the decoded frames queue and then reset
  283. * the decoder */
  284. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  285. /* Free data in the output frame queue. */
  286. ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
  287. libschroedinger_decode_frame_free);
  288. ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
  289. schro_decoder_reset(p_schro_params->decoder);
  290. p_schro_params->eos_pulled = 0;
  291. p_schro_params->eos_signalled = 0;
  292. }
  293. AVCodec libschroedinger_decoder = {
  294. "libschroedinger",
  295. CODEC_TYPE_VIDEO,
  296. CODEC_ID_DIRAC,
  297. sizeof(FfmpegSchroDecoderParams),
  298. libschroedinger_decode_init,
  299. NULL,
  300. libschroedinger_decode_close,
  301. libschroedinger_decode_frame,
  302. CODEC_CAP_DELAY,
  303. .flush = libschroedinger_flush,
  304. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  305. };