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.

172 lines
5.3KB

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