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.

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