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.

397 lines
14KB

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