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.

178 lines
5.4KB

  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 "libavutil/avassert.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/avutil.h"
  27. #include "avcodec.h"
  28. #include "libavutil/imgutils.h"
  29. #include "internal.h"
  30. #include "davs2.h"
  31. typedef struct DAVS2Context {
  32. void *decoder;
  33. AVFrame *frame;
  34. davs2_param_t param; // decoding parameters
  35. davs2_packet_t packet; // input bitstream
  36. int decoded_frames;
  37. davs2_picture_t out_frame; // output data, frame data
  38. davs2_seq_info_t headerset; // output data, sequence header
  39. }DAVS2Context;
  40. static av_cold int davs2_init(AVCodecContext *avctx)
  41. {
  42. DAVS2Context *cad = avctx->priv_data;
  43. /* init the decoder */
  44. cad->param.threads = avctx->thread_count;
  45. cad->param.info_level = 0;
  46. cad->decoder = davs2_decoder_open(&cad->param);
  47. if (!cad->decoder) {
  48. av_log(avctx, AV_LOG_ERROR, "decoder created error.");
  49. return AVERROR(EINVAL);
  50. }
  51. av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
  52. return 0;
  53. }
  54. static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic,
  55. davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
  56. {
  57. DAVS2Context *cad = avctx->priv_data;
  58. int bytes_per_sample = pic->bytes_per_sample;
  59. int plane = 0;
  60. int line = 0;
  61. if (!headerset)
  62. return 0;
  63. if (!pic || ret_type == DAVS2_GOT_HEADER) {
  64. avctx->width = headerset->width;
  65. avctx->height = headerset->height;
  66. avctx->pix_fmt = headerset->output_bit_depth == 10 ?
  67. AV_PIX_FMT_YUV420P10 : AV_PIX_FMT_YUV420P;
  68. avctx->framerate = av_d2q(headerset->frame_rate,4096);
  69. return 0;
  70. }
  71. for (plane = 0; plane < 3; ++plane) {
  72. int size_line = pic->widths[plane] * bytes_per_sample;
  73. frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
  74. if (!frame->buf[plane]){
  75. av_log(avctx, AV_LOG_ERROR, "dump error: alloc failed.\n");
  76. return AVERROR(EINVAL);
  77. }
  78. frame->data[plane] = frame->buf[plane]->data;
  79. frame->linesize[plane] = pic->widths[plane];
  80. for (line = 0; line < pic->lines[plane]; ++line)
  81. memcpy(frame->data[plane] + line * size_line,
  82. pic->planes[plane] + line * pic->strides[plane],
  83. pic->widths[plane] * bytes_per_sample);
  84. }
  85. frame->width = cad->headerset.width;
  86. frame->height = cad->headerset.height;
  87. frame->pts = cad->out_frame.pts;
  88. frame->pict_type = pic->type;
  89. frame->format = avctx->pix_fmt;
  90. cad->decoded_frames++;
  91. return 1;
  92. }
  93. static av_cold int davs2_end(AVCodecContext *avctx)
  94. {
  95. DAVS2Context *cad = avctx->priv_data;
  96. /* close the decoder */
  97. if (cad->decoder) {
  98. davs2_decoder_close(cad->decoder);
  99. cad->decoder = NULL;
  100. }
  101. return 0;
  102. }
  103. static int davs2_decode_frame(AVCodecContext *avctx, void *data,
  104. int *got_frame, AVPacket *avpkt)
  105. {
  106. DAVS2Context *cad = avctx->priv_data;
  107. int buf_size = avpkt->size;
  108. uint8_t *buf_ptr = avpkt->data;
  109. AVFrame *frame = data;
  110. int ret = DAVS2_DEFAULT;
  111. if (!buf_size) {
  112. return 0;
  113. }
  114. cad->packet.data = buf_ptr;
  115. cad->packet.len = buf_size;
  116. cad->packet.pts = avpkt->pts;
  117. cad->packet.dts = avpkt->dts;
  118. ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
  119. if (ret == DAVS2_ERROR) {
  120. av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
  121. return AVERROR(EINVAL);
  122. }
  123. ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
  124. if (ret != DAVS2_DEFAULT) {
  125. *got_frame = davs2_dump_frames(avctx, &cad->out_frame, &cad->headerset, ret, frame);
  126. davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
  127. }
  128. return buf_size;
  129. }
  130. AVCodec ff_libdavs2_decoder = {
  131. .name = "libdavs2",
  132. .long_name = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .id = AV_CODEC_ID_AVS2,
  135. .priv_data_size = sizeof(DAVS2Context),
  136. .init = davs2_init,
  137. .close = davs2_end,
  138. .decode = davs2_decode_frame,
  139. .capabilities = AV_CODEC_CAP_DELAY,//AV_CODEC_CAP_DR1 |
  140. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10,
  141. AV_PIX_FMT_NONE },
  142. .wrapper_name = "libdavs2",
  143. };