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.

188 lines
5.8KB

  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. /* init the decoder */
  38. cad->param.threads = avctx->thread_count;
  39. cad->param.info_level = 0;
  40. cad->decoder = davs2_decoder_open(&cad->param);
  41. if (!cad->decoder) {
  42. av_log(avctx, AV_LOG_ERROR, "decoder created error.");
  43. return AVERROR_EXTERNAL;
  44. }
  45. av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
  46. return 0;
  47. }
  48. static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic,
  49. davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
  50. {
  51. DAVS2Context *cad = avctx->priv_data;
  52. int bytes_per_sample = pic->bytes_per_sample;
  53. int plane = 0;
  54. int line = 0;
  55. if (!headerset)
  56. return 0;
  57. if (!pic || ret_type == DAVS2_GOT_HEADER) {
  58. avctx->width = headerset->width;
  59. avctx->height = headerset->height;
  60. avctx->pix_fmt = headerset->output_bit_depth == 10 ?
  61. AV_PIX_FMT_YUV420P10 : AV_PIX_FMT_YUV420P;
  62. avctx->framerate = av_d2q(headerset->frame_rate,4096);
  63. return 0;
  64. }
  65. switch (pic->type) {
  66. case DAVS2_PIC_I:
  67. case DAVS2_PIC_G:
  68. frame->pict_type = AV_PICTURE_TYPE_I;
  69. break;
  70. case DAVS2_PIC_P:
  71. case DAVS2_PIC_S:
  72. frame->pict_type = AV_PICTURE_TYPE_P;
  73. break;
  74. case DAVS2_PIC_B:
  75. frame->pict_type = AV_PICTURE_TYPE_B;
  76. break;
  77. case DAVS2_PIC_F:
  78. frame->pict_type = AV_PICTURE_TYPE_S;
  79. break;
  80. default:
  81. av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
  82. return AVERROR_EXTERNAL;
  83. }
  84. for (plane = 0; plane < 3; ++plane) {
  85. int size_line = pic->widths[plane] * bytes_per_sample;
  86. frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
  87. if (!frame->buf[plane]){
  88. av_log(avctx, AV_LOG_ERROR, "dump error: alloc failed.\n");
  89. return AVERROR(ENOMEM);
  90. }
  91. frame->data[plane] = frame->buf[plane]->data;
  92. frame->linesize[plane] = size_line;
  93. for (line = 0; line < pic->lines[plane]; ++line)
  94. memcpy(frame->data[plane] + line * size_line,
  95. pic->planes[plane] + line * pic->strides[plane],
  96. pic->widths[plane] * bytes_per_sample);
  97. }
  98. frame->width = cad->headerset.width;
  99. frame->height = cad->headerset.height;
  100. frame->pts = cad->out_frame.pts;
  101. frame->format = avctx->pix_fmt;
  102. return 1;
  103. }
  104. static av_cold int davs2_end(AVCodecContext *avctx)
  105. {
  106. DAVS2Context *cad = avctx->priv_data;
  107. /* close the decoder */
  108. if (cad->decoder) {
  109. davs2_decoder_close(cad->decoder);
  110. cad->decoder = NULL;
  111. }
  112. return 0;
  113. }
  114. static int davs2_decode_frame(AVCodecContext *avctx, void *data,
  115. int *got_frame, AVPacket *avpkt)
  116. {
  117. DAVS2Context *cad = avctx->priv_data;
  118. int buf_size = avpkt->size;
  119. uint8_t *buf_ptr = avpkt->data;
  120. AVFrame *frame = data;
  121. int ret = DAVS2_DEFAULT;
  122. if (!buf_size) {
  123. return 0;
  124. }
  125. cad->packet.data = buf_ptr;
  126. cad->packet.len = buf_size;
  127. cad->packet.pts = avpkt->pts;
  128. cad->packet.dts = avpkt->dts;
  129. ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
  130. if (ret == DAVS2_ERROR) {
  131. av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
  132. return AVERROR_EXTERNAL;
  133. }
  134. ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
  135. if (ret != DAVS2_DEFAULT) {
  136. *got_frame = davs2_dump_frames(avctx, &cad->out_frame, &cad->headerset, ret, frame);
  137. davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
  138. }
  139. return buf_size;
  140. }
  141. AVCodec ff_libdavs2_decoder = {
  142. .name = "libdavs2",
  143. .long_name = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
  144. .type = AVMEDIA_TYPE_VIDEO,
  145. .id = AV_CODEC_ID_AVS2,
  146. .priv_data_size = sizeof(DAVS2Context),
  147. .init = davs2_init,
  148. .close = davs2_end,
  149. .decode = davs2_decode_frame,
  150. .capabilities = AV_CODEC_CAP_DELAY,//AV_CODEC_CAP_DR1 |
  151. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  152. AV_PIX_FMT_NONE },
  153. .wrapper_name = "libdavs2",
  154. };