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.

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