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.

229 lines
7.2KB

  1. /*
  2. * AVS2 decoding using the davs2 library
  3. *
  4. * Copyright (C) 2018 Yiqun Xu, <yiqun.xu@vipl.ict.ac.cn>
  5. * Falei Luo, <falei.luo@gmail.com>
  6. * Huiwen Ren, <hwrenx@gmail.com>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "avcodec.h"
  25. #include "davs2.h"
  26. typedef struct DAVS2Context {
  27. void *decoder;
  28. AVFrame *frame;
  29. davs2_param_t param; // decoding parameters
  30. davs2_packet_t packet; // input bitstream
  31. davs2_picture_t out_frame; // output data, frame data
  32. davs2_seq_info_t headerset; // output data, sequence header
  33. }DAVS2Context;
  34. static av_cold int davs2_init(AVCodecContext *avctx)
  35. {
  36. DAVS2Context *cad = avctx->priv_data;
  37. int cpu_flags = av_get_cpu_flags();
  38. /* init the decoder */
  39. cad->param.threads = avctx->thread_count;
  40. cad->param.info_level = 0;
  41. cad->param.disable_avx = !(cpu_flags & AV_CPU_FLAG_AVX &&
  42. cpu_flags & AV_CPU_FLAG_AVX2);
  43. cad->decoder = davs2_decoder_open(&cad->param);
  44. if (!cad->decoder) {
  45. av_log(avctx, AV_LOG_ERROR, "decoder created error.");
  46. return AVERROR_EXTERNAL;
  47. }
  48. av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
  49. return 0;
  50. }
  51. static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic, int *got_frame,
  52. davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
  53. {
  54. DAVS2Context *cad = avctx->priv_data;
  55. int bytes_per_sample = pic->bytes_per_sample;
  56. int plane = 0;
  57. int line = 0;
  58. if (!headerset) {
  59. *got_frame = 0;
  60. return 0;
  61. }
  62. if (!pic || ret_type == DAVS2_GOT_HEADER) {
  63. avctx->width = headerset->width;
  64. avctx->height = headerset->height;
  65. avctx->pix_fmt = headerset->output_bit_depth == 10 ?
  66. AV_PIX_FMT_YUV420P10 : AV_PIX_FMT_YUV420P;
  67. avctx->framerate = av_d2q(headerset->frame_rate,4096);
  68. *got_frame = 0;
  69. return 0;
  70. }
  71. switch (pic->type) {
  72. case DAVS2_PIC_I:
  73. case DAVS2_PIC_G:
  74. frame->pict_type = AV_PICTURE_TYPE_I;
  75. break;
  76. case DAVS2_PIC_P:
  77. case DAVS2_PIC_S:
  78. frame->pict_type = AV_PICTURE_TYPE_P;
  79. break;
  80. case DAVS2_PIC_B:
  81. frame->pict_type = AV_PICTURE_TYPE_B;
  82. break;
  83. case DAVS2_PIC_F:
  84. frame->pict_type = AV_PICTURE_TYPE_S;
  85. break;
  86. default:
  87. av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
  88. return AVERROR_EXTERNAL;
  89. }
  90. for (plane = 0; plane < 3; ++plane) {
  91. int size_line = pic->widths[plane] * bytes_per_sample;
  92. frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
  93. if (!frame->buf[plane]){
  94. av_log(avctx, AV_LOG_ERROR, "Decoder error: allocation failure, can't dump frames.\n");
  95. return AVERROR(ENOMEM);
  96. }
  97. frame->data[plane] = frame->buf[plane]->data;
  98. frame->linesize[plane] = size_line;
  99. for (line = 0; line < pic->lines[plane]; ++line)
  100. memcpy(frame->data[plane] + line * size_line,
  101. pic->planes[plane] + line * pic->strides[plane],
  102. pic->widths[plane] * bytes_per_sample);
  103. }
  104. frame->width = cad->headerset.width;
  105. frame->height = cad->headerset.height;
  106. frame->pts = cad->out_frame.pts;
  107. frame->format = avctx->pix_fmt;
  108. *got_frame = 1;
  109. return 0;
  110. }
  111. static void davs2_flush(AVCodecContext *avctx)
  112. {
  113. DAVS2Context *cad = avctx->priv_data;
  114. int ret = DAVS2_GOT_FRAME;
  115. while (ret == DAVS2_GOT_FRAME) {
  116. ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
  117. davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
  118. }
  119. if (ret == DAVS2_ERROR) {
  120. av_log(avctx, AV_LOG_WARNING, "Decoder flushing failed.\n");
  121. }
  122. }
  123. static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
  124. {
  125. DAVS2Context *cad = avctx->priv_data;
  126. int ret = DAVS2_DEFAULT;
  127. ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
  128. if (ret == DAVS2_ERROR) {
  129. av_log(avctx, AV_LOG_ERROR, "Decoder error: can't flush delayed frame\n");
  130. return AVERROR_EXTERNAL;
  131. }
  132. if (ret == DAVS2_GOT_FRAME) {
  133. ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
  134. davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
  135. }
  136. return ret;
  137. }
  138. static av_cold int davs2_end(AVCodecContext *avctx)
  139. {
  140. DAVS2Context *cad = avctx->priv_data;
  141. /* close the decoder */
  142. if (cad->decoder) {
  143. davs2_decoder_close(cad->decoder);
  144. cad->decoder = NULL;
  145. }
  146. return 0;
  147. }
  148. static int davs2_decode_frame(AVCodecContext *avctx, void *data,
  149. int *got_frame, AVPacket *avpkt)
  150. {
  151. DAVS2Context *cad = avctx->priv_data;
  152. int buf_size = avpkt->size;
  153. uint8_t *buf_ptr = avpkt->data;
  154. AVFrame *frame = data;
  155. int ret = DAVS2_DEFAULT;
  156. /* end of stream, output what is still in the buffers */
  157. if (!buf_size) {
  158. return send_delayed_frame(avctx, frame, got_frame);
  159. }
  160. cad->packet.data = buf_ptr;
  161. cad->packet.len = buf_size;
  162. cad->packet.pts = avpkt->pts;
  163. cad->packet.dts = avpkt->dts;
  164. ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
  165. if (ret == DAVS2_ERROR) {
  166. av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
  167. return AVERROR_EXTERNAL;
  168. }
  169. ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
  170. if (ret != DAVS2_DEFAULT) {
  171. ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
  172. davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
  173. }
  174. return ret == 0 ? buf_size : ret;
  175. }
  176. AVCodec ff_libdavs2_decoder = {
  177. .name = "libdavs2",
  178. .long_name = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .id = AV_CODEC_ID_AVS2,
  181. .priv_data_size = sizeof(DAVS2Context),
  182. .init = davs2_init,
  183. .close = davs2_end,
  184. .decode = davs2_decode_frame,
  185. .flush = davs2_flush,
  186. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  187. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  188. AV_PIX_FMT_NONE },
  189. .wrapper_name = "libdavs2",
  190. };