FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libdavs2.c
Go to the documentation of this file.
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 
25 #include "avcodec.h"
26 #include "davs2.h"
27 
28 typedef struct DAVS2Context {
29  void *decoder;
30 
32  davs2_param_t param; // decoding parameters
33  davs2_packet_t packet; // input bitstream
34 
36 
37  davs2_picture_t out_frame; // output data, frame data
38  davs2_seq_info_t headerset; // output data, sequence header
39 
41 
42 static av_cold int davs2_init(AVCodecContext *avctx)
43 {
44  DAVS2Context *cad = avctx->priv_data;
45 
46  /* init the decoder */
47  cad->param.threads = avctx->thread_count;
48  cad->param.info_level = 0;
49  cad->decoder = davs2_decoder_open(&cad->param);
50 
51  if (!cad->decoder) {
52  av_log(avctx, AV_LOG_ERROR, "decoder created error.");
53  return AVERROR_EXTERNAL;
54  }
55 
56  av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
57  return 0;
58 }
59 
60 static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic,
61  davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
62 {
63  DAVS2Context *cad = avctx->priv_data;
64  int bytes_per_sample = pic->bytes_per_sample;
65  int plane = 0;
66  int line = 0;
67 
68  if (!headerset)
69  return 0;
70 
71  if (!pic || ret_type == DAVS2_GOT_HEADER) {
72  avctx->width = headerset->width;
73  avctx->height = headerset->height;
74  avctx->pix_fmt = headerset->output_bit_depth == 10 ?
76 
77  avctx->framerate = av_d2q(headerset->frame_rate,4096);
78  return 0;
79  }
80 
81  for (plane = 0; plane < 3; ++plane) {
82  int size_line = pic->widths[plane] * bytes_per_sample;
83  frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
84 
85  if (!frame->buf[plane]){
86  av_log(avctx, AV_LOG_ERROR, "dump error: alloc failed.\n");
87  return AVERROR(ENOMEM);
88  }
89 
90  frame->data[plane] = frame->buf[plane]->data;
91  frame->linesize[plane] = pic->widths[plane];
92 
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 
99  frame->width = cad->headerset.width;
100  frame->height = cad->headerset.height;
101  frame->pts = cad->out_frame.pts;
102  frame->pict_type = pic->type;
103  frame->format = avctx->pix_fmt;
104 
105  cad->decoded_frames++;
106  return 1;
107 }
108 
109 static av_cold int davs2_end(AVCodecContext *avctx)
110 {
111  DAVS2Context *cad = avctx->priv_data;
112 
113  /* close the decoder */
114  if (cad->decoder) {
115  davs2_decoder_close(cad->decoder);
116  cad->decoder = NULL;
117  }
118 
119  return 0;
120 }
121 
122 static int davs2_decode_frame(AVCodecContext *avctx, void *data,
123  int *got_frame, AVPacket *avpkt)
124 {
125  DAVS2Context *cad = avctx->priv_data;
126  int buf_size = avpkt->size;
127  uint8_t *buf_ptr = avpkt->data;
128  AVFrame *frame = data;
129  int ret = DAVS2_DEFAULT;
130 
131  if (!buf_size) {
132  return 0;
133  }
134 
135  cad->packet.data = buf_ptr;
136  cad->packet.len = buf_size;
137  cad->packet.pts = avpkt->pts;
138  cad->packet.dts = avpkt->dts;
139 
140  ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
141 
142 
143  if (ret == DAVS2_ERROR) {
144  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
145  return AVERROR_EXTERNAL;
146  }
147 
148  ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
149 
150  if (ret != DAVS2_DEFAULT) {
151  *got_frame = davs2_dump_frames(avctx, &cad->out_frame, &cad->headerset, ret, frame);
152  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
153  }
154 
155  return buf_size;
156 }
157 
159  .name = "libdavs2",
160  .long_name = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
161  .type = AVMEDIA_TYPE_VIDEO,
162  .id = AV_CODEC_ID_AVS2,
163  .priv_data_size = sizeof(DAVS2Context),
164  .init = davs2_init,
165  .close = davs2_end,
167  .capabilities = AV_CODEC_CAP_DELAY,//AV_CODEC_CAP_DR1 |
168  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
169  AV_PIX_FMT_NONE },
170  .wrapper_name = "libdavs2",
171 };
int plane
Definition: avisynth_c.h:422
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3056
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
davs2_picture_t out_frame
Definition: libdavs2.c:37
AVFrame * frame
Definition: libdavs2.c:31
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:418
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
davs2_packet_t packet
Definition: libdavs2.c:33
int size
Definition: avcodec.h:1446
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
AVCodec.
Definition: avcodec.h:3424
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:993
uint8_t
#define av_cold
Definition: attributes.h:82
AVCodec ff_libdavs2_decoder
Definition: libdavs2.c:158
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1445
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
int width
Definition: frame.h:284
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static av_cold int davs2_end(AVCodecContext *avctx)
Definition: libdavs2.c:109
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
Definition: graph2dot.c:48
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:309
int width
picture width / height.
Definition: avcodec.h:1706
static av_cold int davs2_init(AVCodecContext *avctx)
Definition: libdavs2.c:42
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2785
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:299
Libavcodec external API header.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
main external API structure.
Definition: avcodec.h:1533
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:379
void * decoder
Definition: libdavs2.c:29
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
davs2_seq_info_t headerset
Definition: libdavs2.c:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
static int davs2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libdavs2.c:122
int decoded_frames
Definition: libdavs2.c:35
static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic, davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
Definition: libdavs2.c:60
void * priv_data
Definition: avcodec.h:1560
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1444
int height
Definition: frame.h:284
davs2_param_t param
Definition: libdavs2.c:32
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1422
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438