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.

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