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.

391 lines
13KB

  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. } SchroDecoderParams;
  61. typedef struct SchroParseUnitContext {
  62. const uint8_t *buf;
  63. int buf_size;
  64. } SchroParseUnitContext;
  65. static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
  66. void *priv)
  67. {
  68. av_freep(&priv);
  69. }
  70. static void parse_context_init(SchroParseUnitContext *parse_ctx,
  71. const uint8_t *buf, int buf_size)
  72. {
  73. parse_ctx->buf = buf;
  74. parse_ctx->buf_size = buf_size;
  75. }
  76. static SchroBuffer *find_next_parse_unit(SchroParseUnitContext *parse_ctx)
  77. {
  78. SchroBuffer *enc_buf = NULL;
  79. int next_pu_offset = 0;
  80. unsigned char *in_buf;
  81. if (parse_ctx->buf_size < 13 ||
  82. parse_ctx->buf[0] != 'B' ||
  83. parse_ctx->buf[1] != 'B' ||
  84. parse_ctx->buf[2] != 'C' ||
  85. parse_ctx->buf[3] != 'D')
  86. return NULL;
  87. next_pu_offset = (parse_ctx->buf[5] << 24) +
  88. (parse_ctx->buf[6] << 16) +
  89. (parse_ctx->buf[7] << 8) +
  90. parse_ctx->buf[8];
  91. if (next_pu_offset == 0 &&
  92. SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
  93. next_pu_offset = 13;
  94. if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
  95. return NULL;
  96. in_buf = av_malloc(next_pu_offset);
  97. if (!in_buf) {
  98. av_log(parse_ctx, AV_LOG_ERROR, "Unable to allocate input buffer\n");
  99. return NULL;
  100. }
  101. memcpy(in_buf, parse_ctx->buf, next_pu_offset);
  102. enc_buf = schro_buffer_new_with_data(in_buf, next_pu_offset);
  103. enc_buf->free = libschroedinger_decode_buffer_free;
  104. enc_buf->priv = in_buf;
  105. parse_ctx->buf += next_pu_offset;
  106. parse_ctx->buf_size -= next_pu_offset;
  107. return enc_buf;
  108. }
  109. /**
  110. * Returns Libav chroma format.
  111. */
  112. static enum AVPixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
  113. {
  114. int num_formats = sizeof(schro_pixel_format_map) /
  115. sizeof(schro_pixel_format_map[0]);
  116. int idx;
  117. for (idx = 0; idx < num_formats; ++idx)
  118. if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt)
  119. return schro_pixel_format_map[idx].ff_pix_fmt;
  120. return AV_PIX_FMT_NONE;
  121. }
  122. static av_cold int libschroedinger_decode_init(AVCodecContext *avctx)
  123. {
  124. SchroDecoderParams *p_schro_params = avctx->priv_data;
  125. /* First of all, initialize our supporting libraries. */
  126. schro_init();
  127. schro_debug_set_level(avctx->debug);
  128. p_schro_params->decoder = schro_decoder_new();
  129. schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
  130. if (!p_schro_params->decoder)
  131. return -1;
  132. /* Initialize the decoded frame queue. */
  133. ff_schro_queue_init(&p_schro_params->dec_frame_queue);
  134. return 0;
  135. }
  136. static void libschroedinger_decode_frame_free(void *frame)
  137. {
  138. schro_frame_unref(frame);
  139. }
  140. static void libschroedinger_handle_first_access_unit(AVCodecContext *avctx)
  141. {
  142. SchroDecoderParams *p_schro_params = avctx->priv_data;
  143. SchroDecoder *decoder = p_schro_params->decoder;
  144. p_schro_params->format = schro_decoder_get_video_format(decoder);
  145. /* Tell Libav about sequence details. */
  146. if (av_image_check_size(p_schro_params->format->width,
  147. p_schro_params->format->height, 0, avctx) < 0) {
  148. av_log(avctx, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
  149. p_schro_params->format->width, p_schro_params->format->height);
  150. avctx->height = avctx->width = 0;
  151. return;
  152. }
  153. avctx->height = p_schro_params->format->height;
  154. avctx->width = p_schro_params->format->width;
  155. avctx->pix_fmt = get_chroma_format(p_schro_params->format->chroma_format);
  156. if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
  157. &p_schro_params->frame_format) == -1) {
  158. av_log(avctx, AV_LOG_ERROR,
  159. "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
  160. "and 4:4:4 formats.\n");
  161. return;
  162. }
  163. avctx->time_base.den = p_schro_params->format->frame_rate_numerator;
  164. avctx->time_base.num = p_schro_params->format->frame_rate_denominator;
  165. }
  166. static int libschroedinger_decode_frame(AVCodecContext *avctx,
  167. void *data, int *got_frame,
  168. AVPacket *avpkt)
  169. {
  170. const uint8_t *buf = avpkt->data;
  171. int buf_size = avpkt->size;
  172. int64_t pts = avpkt->pts;
  173. SchroTag *tag;
  174. SchroDecoderParams *p_schro_params = avctx->priv_data;
  175. SchroDecoder *decoder = p_schro_params->decoder;
  176. SchroBuffer *enc_buf;
  177. SchroFrame* frame;
  178. AVFrame *avframe = data;
  179. int state;
  180. int go = 1;
  181. int outer = 1;
  182. SchroParseUnitContext parse_ctx;
  183. LibSchroFrameContext *framewithpts = NULL;
  184. *got_frame = 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(avctx, 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. avctx->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(avctx);
  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(avctx);
  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(avctx,
  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(avctx, 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 (ff_get_buffer(avctx, avframe, 0) < 0) {
  262. av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
  263. return AVERROR(ENOMEM);
  264. }
  265. memcpy(avframe->data[0],
  266. framewithpts->frame->components[0].data,
  267. framewithpts->frame->components[0].length);
  268. memcpy(avframe->data[1],
  269. framewithpts->frame->components[1].data,
  270. framewithpts->frame->components[1].length);
  271. memcpy(avframe->data[2],
  272. framewithpts->frame->components[2].data,
  273. framewithpts->frame->components[2].length);
  274. /* Fill frame with current buffer data from Schroedinger. */
  275. avframe->pkt_pts = framewithpts->pts;
  276. avframe->linesize[0] = framewithpts->frame->components[0].stride;
  277. avframe->linesize[1] = framewithpts->frame->components[1].stride;
  278. avframe->linesize[2] = framewithpts->frame->components[2].stride;
  279. *got_frame = 1;
  280. /* Now free the frame resources. */
  281. libschroedinger_decode_frame_free(framewithpts->frame);
  282. av_free(framewithpts);
  283. } else {
  284. data = NULL;
  285. *got_frame = 0;
  286. }
  287. return buf_size;
  288. }
  289. static av_cold int libschroedinger_decode_close(AVCodecContext *avctx)
  290. {
  291. SchroDecoderParams *p_schro_params = avctx->priv_data;
  292. /* Free the decoder. */
  293. schro_decoder_free(p_schro_params->decoder);
  294. av_freep(&p_schro_params->format);
  295. /* Free data in the output frame queue. */
  296. ff_schro_queue_free(&p_schro_params->dec_frame_queue,
  297. libschroedinger_decode_frame_free);
  298. return 0;
  299. }
  300. static void libschroedinger_flush(AVCodecContext *avctx)
  301. {
  302. /* Got a seek request. Free the decoded frames queue and then reset
  303. * the decoder */
  304. SchroDecoderParams *p_schro_params = avctx->priv_data;
  305. /* Free data in the output frame queue. */
  306. ff_schro_queue_free(&p_schro_params->dec_frame_queue,
  307. libschroedinger_decode_frame_free);
  308. ff_schro_queue_init(&p_schro_params->dec_frame_queue);
  309. schro_decoder_reset(p_schro_params->decoder);
  310. p_schro_params->eos_pulled = 0;
  311. p_schro_params->eos_signalled = 0;
  312. }
  313. AVCodec ff_libschroedinger_decoder = {
  314. .name = "libschroedinger",
  315. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .id = AV_CODEC_ID_DIRAC,
  318. .priv_data_size = sizeof(SchroDecoderParams),
  319. .init = libschroedinger_decode_init,
  320. .close = libschroedinger_decode_close,
  321. .decode = libschroedinger_decode_frame,
  322. .capabilities = CODEC_CAP_DELAY,
  323. .flush = libschroedinger_flush,
  324. };