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.

394 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. #include <schroedinger/schro.h>
  37. #include <schroedinger/schrodebug.h>
  38. #include <schroedinger/schrovideoformat.h>
  39. /** SchroFrame and Pts relation */
  40. typedef struct LibSchroFrameContext {
  41. SchroFrame *frame;
  42. int64_t pts;
  43. } LibSchroFrameContext;
  44. /** libschroedinger decoder private data */
  45. typedef struct SchroDecoderParams {
  46. /** Schroedinger video format */
  47. SchroVideoFormat *format;
  48. /** Schroedinger frame format */
  49. SchroFrameFormat frame_format;
  50. /** decoder handle */
  51. SchroDecoder* decoder;
  52. /** queue storing decoded frames */
  53. FFSchroQueue dec_frame_queue;
  54. /** end of sequence signalled */
  55. int eos_signalled;
  56. /** end of sequence pulled */
  57. int eos_pulled;
  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 AVPixelFormat 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 AV_PIX_FMT_NONE;
  119. }
  120. static av_cold int libschroedinger_decode_init(AVCodecContext *avctx)
  121. {
  122. SchroDecoderParams *p_schro_params = avctx->priv_data;
  123. /* First of all, initialize our supporting libraries. */
  124. schro_init();
  125. schro_debug_set_level(avctx->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 *avctx)
  139. {
  140. SchroDecoderParams *p_schro_params = avctx->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, avctx) < 0) {
  146. av_log(avctx, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
  147. p_schro_params->format->width, p_schro_params->format->height);
  148. avctx->height = avctx->width = 0;
  149. return;
  150. }
  151. avctx->height = p_schro_params->format->height;
  152. avctx->width = p_schro_params->format->width;
  153. avctx->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(avctx, 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. avctx->framerate.num = p_schro_params->format->frame_rate_numerator;
  162. avctx->framerate.den = p_schro_params->format->frame_rate_denominator;
  163. }
  164. static int libschroedinger_decode_frame(AVCodecContext *avctx,
  165. void *data, int *got_frame,
  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 = avctx->priv_data;
  173. SchroDecoder *decoder = p_schro_params->decoder;
  174. SchroBuffer *enc_buf;
  175. SchroFrame* frame;
  176. AVFrame *avframe = data;
  177. int state;
  178. int go = 1;
  179. int outer = 1;
  180. SchroParseUnitContext parse_ctx;
  181. LibSchroFrameContext *framewithpts = NULL;
  182. *got_frame = 0;
  183. parse_context_init(&parse_ctx, buf, buf_size);
  184. if (!buf_size) {
  185. if (!p_schro_params->eos_signalled) {
  186. state = schro_decoder_push_end_of_stream(decoder);
  187. p_schro_params->eos_signalled = 1;
  188. }
  189. }
  190. /* Loop through all the individual parse units in the input buffer */
  191. do {
  192. if ((enc_buf = find_next_parse_unit(&parse_ctx))) {
  193. /* Set Schrotag with the pts to be recovered after decoding*/
  194. enc_buf->tag = schro_tag_new(av_malloc(sizeof(int64_t)), av_free);
  195. if (!enc_buf->tag->value) {
  196. av_log(avctx, AV_LOG_ERROR, "Unable to allocate SchroTag\n");
  197. return AVERROR(ENOMEM);
  198. }
  199. AV_WN(64, enc_buf->tag->value, pts);
  200. /* Push buffer into decoder. */
  201. if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
  202. SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
  203. avctx->has_b_frames = 1;
  204. state = schro_decoder_push(decoder, enc_buf);
  205. if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
  206. libschroedinger_handle_first_access_unit(avctx);
  207. go = 1;
  208. } else
  209. outer = 0;
  210. while (go) {
  211. /* Parse data and process result. */
  212. state = schro_decoder_wait(decoder);
  213. switch (state) {
  214. case SCHRO_DECODER_FIRST_ACCESS_UNIT:
  215. libschroedinger_handle_first_access_unit(avctx);
  216. break;
  217. case SCHRO_DECODER_NEED_BITS:
  218. /* Need more input data - stop iterating over what we have. */
  219. go = 0;
  220. break;
  221. case SCHRO_DECODER_NEED_FRAME:
  222. /* Decoder needs a frame - create one and push it in. */
  223. frame = ff_create_schro_frame(avctx,
  224. p_schro_params->frame_format);
  225. if (!frame)
  226. return AVERROR(ENOMEM);
  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->pts = framewithpts->pts;
  276. #if FF_API_PKT_PTS
  277. FF_DISABLE_DEPRECATION_WARNINGS
  278. avframe->pkt_pts = avframe->pts;
  279. FF_ENABLE_DEPRECATION_WARNINGS
  280. #endif
  281. avframe->linesize[0] = framewithpts->frame->components[0].stride;
  282. avframe->linesize[1] = framewithpts->frame->components[1].stride;
  283. avframe->linesize[2] = framewithpts->frame->components[2].stride;
  284. *got_frame = 1;
  285. /* Now free the frame resources. */
  286. libschroedinger_decode_frame_free(framewithpts->frame);
  287. av_free(framewithpts);
  288. } else {
  289. data = NULL;
  290. *got_frame = 0;
  291. }
  292. return buf_size;
  293. }
  294. static av_cold int libschroedinger_decode_close(AVCodecContext *avctx)
  295. {
  296. SchroDecoderParams *p_schro_params = avctx->priv_data;
  297. /* Free the decoder. */
  298. schro_decoder_free(p_schro_params->decoder);
  299. av_freep(&p_schro_params->format);
  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 *avctx)
  306. {
  307. /* Got a seek request. Free the decoded frames queue and then reset
  308. * the decoder */
  309. SchroDecoderParams *p_schro_params = avctx->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. .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .id = AV_CODEC_ID_DIRAC,
  323. .priv_data_size = sizeof(SchroDecoderParams),
  324. .init = libschroedinger_decode_init,
  325. .close = libschroedinger_decode_close,
  326. .decode = libschroedinger_decode_frame,
  327. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  328. .flush = libschroedinger_flush,
  329. };