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.

375 lines
12KB

  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. AVPacket *avpkt)
  171. {
  172. const uint8_t *buf = avpkt->data;
  173. int buf_size = avpkt->size;
  174. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  175. SchroDecoder *decoder = p_schro_params->decoder;
  176. SchroVideoFormat *format;
  177. AVPicture *picture = data;
  178. SchroBuffer *enc_buf;
  179. SchroFrame* frame;
  180. int state;
  181. int go = 1;
  182. int outer = 1;
  183. FfmpegSchroParseUnitContext parse_ctx;
  184. *data_size = 0;
  185. FfmpegSchroParseContextInit (&parse_ctx, buf, buf_size);
  186. if (buf_size == 0) {
  187. if (!p_schro_params->eos_signalled) {
  188. state = schro_decoder_push_end_of_stream(decoder);
  189. p_schro_params->eos_signalled = 1;
  190. }
  191. }
  192. /* Loop through all the individual parse units in the input buffer */
  193. do {
  194. if ((enc_buf = FfmpegFindNextSchroParseUnit(&parse_ctx))) {
  195. /* Push buffer into decoder. */
  196. if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
  197. SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
  198. avccontext->has_b_frames = 1;
  199. state = schro_decoder_push (decoder, enc_buf);
  200. if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
  201. libschroedinger_handle_first_access_unit(avccontext);
  202. go = 1;
  203. }
  204. else
  205. outer = 0;
  206. format = p_schro_params->format;
  207. while (go) {
  208. /* Parse data and process result. */
  209. state = schro_decoder_wait (decoder);
  210. switch (state)
  211. {
  212. case SCHRO_DECODER_FIRST_ACCESS_UNIT:
  213. libschroedinger_handle_first_access_unit (avccontext);
  214. break;
  215. case SCHRO_DECODER_NEED_BITS:
  216. /* Need more input data - stop iterating over what we have. */
  217. go = 0;
  218. break;
  219. case SCHRO_DECODER_NEED_FRAME:
  220. /* Decoder needs a frame - create one and push it in. */
  221. frame = schro_frame_new_and_alloc(NULL,
  222. p_schro_params->frame_format,
  223. format->width,
  224. format->height);
  225. schro_decoder_add_output_picture (decoder, frame);
  226. break;
  227. case SCHRO_DECODER_OK:
  228. /* Pull a frame out of the decoder. */
  229. frame = schro_decoder_pull (decoder);
  230. if (frame) {
  231. ff_dirac_schro_queue_push_back(
  232. &p_schro_params->dec_frame_queue,
  233. frame);
  234. }
  235. break;
  236. case SCHRO_DECODER_EOS:
  237. go = 0;
  238. p_schro_params->eos_pulled = 1;
  239. schro_decoder_reset (decoder);
  240. outer = 0;
  241. break;
  242. case SCHRO_DECODER_ERROR:
  243. return -1;
  244. break;
  245. }
  246. }
  247. } while(outer);
  248. /* Grab next frame to be returned from the top of the queue. */
  249. frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
  250. if (frame != NULL) {
  251. memcpy (p_schro_params->dec_pic.data[0],
  252. frame->components[0].data,
  253. frame->components[0].length);
  254. memcpy (p_schro_params->dec_pic.data[1],
  255. frame->components[1].data,
  256. frame->components[1].length);
  257. memcpy (p_schro_params->dec_pic.data[2],
  258. frame->components[2].data,
  259. frame->components[2].length);
  260. /* Fill picture with current buffer data from Schroedinger. */
  261. avpicture_fill(picture, p_schro_params->dec_pic.data[0],
  262. avccontext->pix_fmt,
  263. avccontext->width, avccontext->height);
  264. *data_size = sizeof(AVPicture);
  265. /* Now free the frame resources. */
  266. libschroedinger_decode_frame_free (frame);
  267. }
  268. return buf_size;
  269. }
  270. static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
  271. {
  272. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  273. /* Free the decoder. */
  274. schro_decoder_free (p_schro_params->decoder);
  275. av_freep(&p_schro_params->format);
  276. avpicture_free (&p_schro_params->dec_pic);
  277. /* Free data in the output frame queue. */
  278. ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
  279. libschroedinger_decode_frame_free);
  280. return 0 ;
  281. }
  282. static void libschroedinger_flush (AVCodecContext *avccontext)
  283. {
  284. /* Got a seek request. Free the decoded frames queue and then reset
  285. * the decoder */
  286. FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
  287. /* Free data in the output frame queue. */
  288. ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
  289. libschroedinger_decode_frame_free);
  290. ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
  291. schro_decoder_reset(p_schro_params->decoder);
  292. p_schro_params->eos_pulled = 0;
  293. p_schro_params->eos_signalled = 0;
  294. }
  295. AVCodec libschroedinger_decoder = {
  296. "libschroedinger",
  297. CODEC_TYPE_VIDEO,
  298. CODEC_ID_DIRAC,
  299. sizeof(FfmpegSchroDecoderParams),
  300. libschroedinger_decode_init,
  301. NULL,
  302. libschroedinger_decode_close,
  303. libschroedinger_decode_frame,
  304. CODEC_CAP_DELAY,
  305. .flush = libschroedinger_flush,
  306. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  307. };