2 * OpenH264 video decoder
3 * Copyright (C) 2016 Martin Storsjo
5 * This file is part of FFmpeg.
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.
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.
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
22 #include <wels/codec_api.h>
23 #include <wels/codec_ver.h>
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"
33 #include "codec_internal.h"
35 #include "libopenh264.h"
37 typedef struct SVCContext {
41 static av_cold int svc_decode_close(AVCodecContext *avctx)
43 SVCContext *s = avctx->priv_data;
46 WelsDestroyDecoder(s->decoder);
51 static av_cold int svc_decode_init(AVCodecContext *avctx)
53 SVCContext *s = avctx->priv_data;
54 SDecodingParam param = { 0 };
57 WelsTraceCallback callback_function;
59 if ((err = ff_libopenh264_check_version(avctx)) < 0)
60 return AVERROR_DECODER_NOT_FOUND;
62 if (WelsCreateDecoder(&s->decoder)) {
63 av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
64 return AVERROR_UNKNOWN;
67 // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
68 log_level = WELS_LOG_DETAIL;
69 callback_function = ff_libopenh264_trace_callback;
70 (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
71 (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
72 (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
74 #if !OPENH264_VER_AT_LEAST(1, 6)
75 param.eOutputColorFormat = videoFormatI420;
77 param.eEcActiveIdc = ERROR_CON_DISABLE;
78 param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
80 if ((*s->decoder)->Initialize(s->decoder, ¶m) != cmResultSuccess) {
81 av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
82 return AVERROR_UNKNOWN;
85 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
90 static int svc_decode_frame(AVCodecContext *avctx, AVFrame *avframe,
91 int *got_frame, AVPacket *avpkt)
93 SVCContext *s = avctx->priv_data;
94 SBufferInfo info = { 0 };
95 uint8_t *ptrs[4] = { NULL };
98 #if OPENH264_VER_AT_LEAST(1, 7)
103 #if OPENH264_VER_AT_LEAST(1, 9)
104 int end_of_stream = 1;
105 (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_END_OF_STREAM, &end_of_stream);
106 state = (*s->decoder)->FlushFrame(s->decoder, ptrs, &info);
111 info.uiInBsTimeStamp = avpkt->pts;
112 #if OPENH264_VER_AT_LEAST(1, 4)
113 // Contrary to the name, DecodeFrameNoDelay actually does buffering
114 // and reordering of frames, and is the recommended decoding entry
115 // point since 1.4. This is essential for successfully decoding
117 state = (*s->decoder)->DecodeFrameNoDelay(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
119 state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
122 if (state != dsErrorFree) {
123 av_log(avctx, AV_LOG_ERROR, "DecodeFrame failed\n");
124 return AVERROR_UNKNOWN;
126 if (info.iBufferStatus != 1) {
127 av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
131 ret = ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
134 // The decoder doesn't (currently) support decoding into a user
135 // provided buffer, so do a copy instead.
136 if (ff_get_buffer(avctx, avframe, 0) < 0) {
137 av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
138 return AVERROR(ENOMEM);
141 linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
142 linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
144 av_image_copy2(avframe->data, avframe->linesize, ptrs, linesize,
145 avctx->pix_fmt, avctx->width, avctx->height);
147 avframe->pts = info.uiOutYuvTimeStamp;
148 avframe->pkt_dts = AV_NOPTS_VALUE;
149 #if OPENH264_VER_AT_LEAST(1, 7)
150 (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_PROFILE, &opt);
151 avctx->profile = opt;
152 (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_LEVEL, &opt);
160 const FFCodec ff_libopenh264_decoder = {
161 .p.name = "libopenh264",
162 CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
163 .p.type = AVMEDIA_TYPE_VIDEO,
164 .p.id = AV_CODEC_ID_H264,
165 .priv_data_size = sizeof(SVCContext),
166 .init = svc_decode_init,
167 FF_CODEC_DECODE_CB(svc_decode_frame),
168 .close = svc_decode_close,
169 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
170 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS |
171 FF_CODEC_CAP_INIT_CLEANUP,
172 .bsfs = "h264_mp4toannexb",
173 .p.wrapper_name = "libopenh264",