FFmpeg
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 "codec_internal.h"
34 #include "decode.h"
35 #include "libopenh264.h"
36 
37 typedef struct SVCContext {
38  ISVCDecoder *decoder;
39 } SVCContext;
40 
42 {
43  SVCContext *s = avctx->priv_data;
44 
45  if (s->decoder)
46  WelsDestroyDecoder(s->decoder);
47 
48  return 0;
49 }
50 
52 {
53  SVCContext *s = avctx->priv_data;
54  SDecodingParam param = { 0 };
55  int log_level;
56  WelsTraceCallback callback_function;
57 
58  if (WelsCreateDecoder(&s->decoder)) {
59  av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
60  return AVERROR_UNKNOWN;
61  }
62 
63  // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
64  log_level = WELS_LOG_DETAIL;
65  callback_function = ff_libopenh264_trace_callback;
66  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
67  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
68  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
69 
70 #if !OPENH264_VER_AT_LEAST(1, 6)
71  param.eOutputColorFormat = videoFormatI420;
72 #endif
73  param.eEcActiveIdc = ERROR_CON_DISABLE;
74  param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
75 
76  if ((*s->decoder)->Initialize(s->decoder, &param) != cmResultSuccess) {
77  av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
78  return AVERROR_UNKNOWN;
79  }
80 
81  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
82 
83  return 0;
84 }
85 
86 static int svc_decode_frame(AVCodecContext *avctx, AVFrame *avframe,
87  int *got_frame, AVPacket *avpkt)
88 {
89  SVCContext *s = avctx->priv_data;
90  SBufferInfo info = { 0 };
91  uint8_t *ptrs[4] = { NULL };
92  int ret, linesize[4];
93  DECODING_STATE state;
94 #if OPENH264_VER_AT_LEAST(1, 7)
95  int opt;
96 #endif
97 
98  if (!avpkt->data) {
99 #if OPENH264_VER_AT_LEAST(1, 9)
100  int end_of_stream = 1;
101  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_END_OF_STREAM, &end_of_stream);
102  state = (*s->decoder)->FlushFrame(s->decoder, ptrs, &info);
103 #else
104  return 0;
105 #endif
106  } else {
107  info.uiInBsTimeStamp = avpkt->pts;
108 #if OPENH264_VER_AT_LEAST(1, 4)
109  // Contrary to the name, DecodeFrameNoDelay actually does buffering
110  // and reordering of frames, and is the recommended decoding entry
111  // point since 1.4. This is essential for successfully decoding
112  // B-frames.
113  state = (*s->decoder)->DecodeFrameNoDelay(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
114 #else
115  state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
116 #endif
117  }
118  if (state != dsErrorFree) {
119  av_log(avctx, AV_LOG_ERROR, "DecodeFrame failed\n");
120  return AVERROR_UNKNOWN;
121  }
122  if (info.iBufferStatus != 1) {
123  av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
124  return avpkt->size;
125  }
126 
127  ret = ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
128  if (ret < 0)
129  return ret;
130  // The decoder doesn't (currently) support decoding into a user
131  // provided buffer, so do a copy instead.
132  if (ff_get_buffer(avctx, avframe, 0) < 0) {
133  av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
134  return AVERROR(ENOMEM);
135  }
136 
137  linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
138  linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
139  linesize[3] = 0;
140  av_image_copy2(avframe->data, avframe->linesize, ptrs, linesize,
141  avctx->pix_fmt, avctx->width, avctx->height);
142 
143  avframe->pts = info.uiOutYuvTimeStamp;
144  avframe->pkt_dts = AV_NOPTS_VALUE;
145 #if OPENH264_VER_AT_LEAST(1, 7)
146  (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_PROFILE, &opt);
147  avctx->profile = opt;
148  (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_LEVEL, &opt);
149  avctx->level = opt;
150 #endif
151 
152  *got_frame = 1;
153  return avpkt->size;
154 }
155 
157  .p.name = "libopenh264",
158  CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
159  .p.type = AVMEDIA_TYPE_VIDEO,
160  .p.id = AV_CODEC_ID_H264,
161  .priv_data_size = sizeof(SVCContext),
164  .close = svc_decode_close,
165  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
166  .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS |
168  .bsfs = "h264_mp4toannexb",
169  .p.wrapper_name = "libopenh264",
170 };
SVCContext
Definition: libopenh264dec.c:37
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
svc_decode_init
static av_cold int svc_decode_init(AVCodecContext *avctx)
Definition: libopenh264dec.c:51
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:456
AVPacket::data
uint8_t * data
Definition: packet.h:522
svc_decode_frame
static int svc_decode_frame(AVCodecContext *avctx, AVFrame *avframe, int *got_frame, AVPacket *avpkt)
Definition: libopenh264dec.c:86
FFCodec
Definition: codec_internal.h:127
mathematics.h
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
ff_set_dimensions
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:94
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
libopenh264.h
fifo.h
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
info
MIPS optimizations info
Definition: mips.txt:2
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decode.h
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
ff_libopenh264_decoder
const FFCodec ff_libopenh264_decoder
Definition: libopenh264dec.c:156
NULL
#define NULL
Definition: coverity.c:32
end_of_stream
static int FUNC() end_of_stream(CodedBitstreamContext *ctx, RWContext *rw, H264RawNALUnitHeader *current)
Definition: cbs_h264_syntax_template.c:1254
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:463
state
static struct @385 state
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1783
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1569
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
codec_internal.h
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
svc_decode_close
static av_cold int svc_decode_close(AVCodecContext *avctx)
Definition: libopenh264dec.c:41
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
common.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
FF_CODEC_CAP_SETS_PKT_DTS
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: codec_internal.h:49
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
SVCContext::decoder
ISVCDecoder * decoder
Definition: libopenh264dec.c:38
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:445
av_image_copy2
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition: imgutils.h:184
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1639
ff_libopenh264_trace_callback
void ff_libopenh264_trace_callback(void *ctx, int level, const char *msg)
Definition: libopenh264.c:42
AV_CODEC_CAP_DELAY
#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: codec.h:76
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27