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.

360 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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "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 SchroDecoderParams {
  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. FFSchroQueue 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. } SchroDecoderParams;
  53. typedef struct SchroParseUnitContext {
  54. const uint8_t *buf;
  55. int buf_size;
  56. } SchroParseUnitContext;
  57. static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
  58. void *priv)
  59. {
  60. av_freep(&priv);
  61. }
  62. static void SchroParseContextInit(SchroParseUnitContext *parse_ctx,
  63. const uint8_t *buf, int buf_size)
  64. {
  65. parse_ctx->buf = buf;
  66. parse_ctx->buf_size = buf_size;
  67. }
  68. static SchroBuffer *FindNextSchroParseUnit(SchroParseUnitContext *parse_ctx)
  69. {
  70. SchroBuffer *enc_buf = NULL;
  71. int next_pu_offset = 0;
  72. unsigned char *in_buf;
  73. if (parse_ctx->buf_size < 13 ||
  74. parse_ctx->buf[0] != 'B' ||
  75. parse_ctx->buf[1] != 'B' ||
  76. parse_ctx->buf[2] != 'C' ||
  77. parse_ctx->buf[3] != 'D')
  78. return NULL;
  79. next_pu_offset = (parse_ctx->buf[5] << 24) +
  80. (parse_ctx->buf[6] << 16) +
  81. (parse_ctx->buf[7] << 8) +
  82. parse_ctx->buf[8];
  83. if (next_pu_offset == 0 &&
  84. SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
  85. next_pu_offset = 13;
  86. if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
  87. return NULL;
  88. in_buf = av_malloc(next_pu_offset);
  89. if (!in_buf) {
  90. av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
  91. return NULL;
  92. }
  93. memcpy(in_buf, parse_ctx->buf, next_pu_offset);
  94. enc_buf = schro_buffer_new_with_data(in_buf, next_pu_offset);
  95. enc_buf->free = libschroedinger_decode_buffer_free;
  96. enc_buf->priv = in_buf;
  97. parse_ctx->buf += next_pu_offset;
  98. parse_ctx->buf_size -= next_pu_offset;
  99. return enc_buf;
  100. }
  101. /**
  102. * Returns Libav chroma format.
  103. */
  104. static enum PixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
  105. {
  106. int num_formats = sizeof(schro_pixel_format_map) /
  107. sizeof(schro_pixel_format_map[0]);
  108. int idx;
  109. for (idx = 0; idx < num_formats; ++idx)
  110. if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt)
  111. return schro_pixel_format_map[idx].ff_pix_fmt;
  112. return PIX_FMT_NONE;
  113. }
  114. static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
  115. {
  116. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  117. /* First of all, initialize our supporting libraries. */
  118. schro_init();
  119. schro_debug_set_level(avccontext->debug);
  120. p_schro_params->decoder = schro_decoder_new();
  121. schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
  122. if (!p_schro_params->decoder)
  123. return -1;
  124. /* Initialize the decoded frame queue. */
  125. ff_schro_queue_init(&p_schro_params->dec_frame_queue);
  126. return 0;
  127. }
  128. static void libschroedinger_decode_frame_free(void *frame)
  129. {
  130. schro_frame_unref(frame);
  131. }
  132. static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
  133. {
  134. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  135. SchroDecoder *decoder = p_schro_params->decoder;
  136. p_schro_params->format = schro_decoder_get_video_format(decoder);
  137. /* Tell Libav about sequence details. */
  138. if (av_image_check_size(p_schro_params->format->width, p_schro_params->format->height,
  139. 0, avccontext) < 0) {
  140. av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
  141. p_schro_params->format->width, p_schro_params->format->height);
  142. avccontext->height = avccontext->width = 0;
  143. return;
  144. }
  145. avccontext->height = p_schro_params->format->height;
  146. avccontext->width = p_schro_params->format->width;
  147. avccontext->pix_fmt = get_chroma_format(p_schro_params->format->chroma_format);
  148. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  149. &p_schro_params->frame_format) == -1) {
  150. av_log(avccontext, AV_LOG_ERROR,
  151. "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
  152. "and 4:4:4 formats.\n");
  153. return;
  154. }
  155. avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
  156. avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
  157. if (!p_schro_params->dec_pic.data[0])
  158. avpicture_alloc(&p_schro_params->dec_pic,
  159. avccontext->pix_fmt,
  160. avccontext->width,
  161. avccontext->height);
  162. }
  163. static int libschroedinger_decode_frame(AVCodecContext *avccontext,
  164. void *data, int *data_size,
  165. AVPacket *avpkt)
  166. {
  167. const uint8_t *buf = avpkt->data;
  168. int buf_size = avpkt->size;
  169. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  170. SchroDecoder *decoder = p_schro_params->decoder;
  171. AVPicture *picture = data;
  172. SchroBuffer *enc_buf;
  173. SchroFrame* frame;
  174. int state;
  175. int go = 1;
  176. int outer = 1;
  177. SchroParseUnitContext parse_ctx;
  178. *data_size = 0;
  179. SchroParseContextInit(&parse_ctx, buf, buf_size);
  180. if (!buf_size) {
  181. if (!p_schro_params->eos_signalled) {
  182. state = schro_decoder_push_end_of_stream(decoder);
  183. p_schro_params->eos_signalled = 1;
  184. }
  185. }
  186. /* Loop through all the individual parse units in the input buffer */
  187. do {
  188. if ((enc_buf = FindNextSchroParseUnit(&parse_ctx))) {
  189. /* Push buffer into decoder. */
  190. if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
  191. SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
  192. avccontext->has_b_frames = 1;
  193. state = schro_decoder_push(decoder, enc_buf);
  194. if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
  195. libschroedinger_handle_first_access_unit(avccontext);
  196. go = 1;
  197. } else
  198. outer = 0;
  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_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_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. SchroDecoderParams *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_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. SchroDecoderParams *p_schro_params = avccontext->priv_data;
  274. /* Free data in the output frame queue. */
  275. ff_schro_queue_free(&p_schro_params->dec_frame_queue,
  276. libschroedinger_decode_frame_free);
  277. ff_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 ff_libschroedinger_decoder = {
  283. .name = "libschroedinger",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .id = CODEC_ID_DIRAC,
  286. .priv_data_size = sizeof(SchroDecoderParams),
  287. .init = libschroedinger_decode_init,
  288. .close = libschroedinger_decode_close,
  289. .decode = libschroedinger_decode_frame,
  290. .capabilities = CODEC_CAP_DELAY,
  291. .flush = libschroedinger_flush,
  292. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  293. };