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.

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