FFmpeg  4.0
libopenh264dec.c
Go to the documentation of this file.
1 /*
2  * OpenH264 video decoder
3  * Copyright (C) 2016 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <wels/codec_api.h>
23 #include <wels/codec_ver.h>
24 
25 #include "libavutil/common.h"
26 #include "libavutil/fifo.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 
32 #include "avcodec.h"
33 #include "internal.h"
34 #include "libopenh264.h"
35 
36 typedef struct SVCContext {
37  ISVCDecoder *decoder;
38 } SVCContext;
39 
41 {
42  SVCContext *s = avctx->priv_data;
43 
44  if (s->decoder)
45  WelsDestroyDecoder(s->decoder);
46 
47  return 0;
48 }
49 
51 {
52  SVCContext *s = avctx->priv_data;
53  SDecodingParam param = { 0 };
54  int err;
55  int log_level;
56  WelsTraceCallback callback_function;
57 
58  if ((err = ff_libopenh264_check_version(avctx)) < 0)
59  return err;
60 
61  if (WelsCreateDecoder(&s->decoder)) {
62  av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
63  return AVERROR_UNKNOWN;
64  }
65 
66  // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
67  log_level = WELS_LOG_DETAIL;
68  callback_function = ff_libopenh264_trace_callback;
69  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
70  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
71  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
72 
73 #if !OPENH264_VER_AT_LEAST(1, 6)
74  param.eOutputColorFormat = videoFormatI420;
75 #endif
76  param.eEcActiveIdc = ERROR_CON_DISABLE;
77  param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
78 
79  if ((*s->decoder)->Initialize(s->decoder, &param) != cmResultSuccess) {
80  av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
81  return AVERROR_UNKNOWN;
82  }
83 
84  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
85 
86  return 0;
87 }
88 
89 static int svc_decode_frame(AVCodecContext *avctx, void *data,
90  int *got_frame, AVPacket *avpkt)
91 {
92  SVCContext *s = avctx->priv_data;
93  SBufferInfo info = { 0 };
94  uint8_t* ptrs[3];
95  int ret, linesize[3];
96  AVFrame *avframe = data;
97  DECODING_STATE state;
98 
99  state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
100  if (state != dsErrorFree) {
101  av_log(avctx, AV_LOG_ERROR, "DecodeFrame2 failed\n");
102  return AVERROR_UNKNOWN;
103  }
104  if (info.iBufferStatus != 1) {
105  av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
106  return avpkt->size;
107  }
108 
109  ret = ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
110  if (ret < 0)
111  return ret;
112  // The decoder doesn't (currently) support decoding into a user
113  // provided buffer, so do a copy instead.
114  if (ff_get_buffer(avctx, avframe, 0) < 0) {
115  av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
116  return AVERROR(ENOMEM);
117  }
118 
119  linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
120  linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
121  av_image_copy(avframe->data, avframe->linesize, (const uint8_t **) ptrs, linesize, avctx->pix_fmt, avctx->width, avctx->height);
122 
123  avframe->pts = avpkt->pts;
124  avframe->pkt_dts = avpkt->dts;
125 #if FF_API_PKT_PTS
127  avframe->pkt_pts = avpkt->pts;
129 #endif
130 
131  *got_frame = 1;
132  return avpkt->size;
133 }
134 
136  .name = "libopenh264",
137  .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
138  .type = AVMEDIA_TYPE_VIDEO,
139  .id = AV_CODEC_ID_H264,
140  .priv_data_size = sizeof(SVCContext),
143  .close = svc_decode_close,
144  // The decoder doesn't currently support B-frames, and the decoder's API
145  // doesn't support reordering/delay, but the BSF could incur delay.
146  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
149  .bsfs = "h264_mp4toannexb",
150  .wrapper_name = "libopenh264",
151 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
void ff_libopenh264_trace_callback(void *ctx, int level, const char *msg)
Definition: libopenh264.c:41
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
misc image utilities
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1431
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
AVCodec.
Definition: avcodec.h:3408
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:984
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
static av_cold int svc_decode_close(AVCodecContext *avctx)
uint8_t
#define av_cold
Definition: attributes.h:82
static int svc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
#define av_log(a,...)
#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 svc_decode_init(AVCodecContext *avctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:387
int width
picture width / height.
Definition: avcodec.h:1690
static struct @271 state
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
main external API structure.
Definition: avcodec.h:1518
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1891
a very simple circular buffer FIFO implementation
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: internal.h:55
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:319
AVCodec ff_libopenh264_decoder
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:327
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
common internal and external API header
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void * priv_data
Definition: avcodec.h:1545
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
int ff_libopenh264_check_version(void *logctx)
Definition: libopenh264.c:49
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1429
ISVCDecoder * decoder
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1423