libopenh264dec: Use a newer decoding entry point function
[libav.git] / libavcodec / libopenh264dec.c
CommitLineData
82b75251
MS
1/*
2 * OpenH264 video decoder
3 * Copyright (C) 2016 Martin Storsjo
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
36typedef struct SVCContext {
37 ISVCDecoder *decoder;
82b75251
MS
38} SVCContext;
39
40static av_cold int svc_decode_close(AVCodecContext *avctx)
41{
42 SVCContext *s = avctx->priv_data;
82b75251
MS
43
44 if (s->decoder)
45 WelsDestroyDecoder(s->decoder);
46
82b75251
MS
47 return 0;
48}
49
50static av_cold int svc_decode_init(AVCodecContext *avctx)
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
82b75251
MS
61 if (WelsCreateDecoder(&s->decoder)) {
62 av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
36b380dc 63 return AVERROR_UNKNOWN;
82b75251
MS
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
d825b1a5 73#if !OPENH264_VER_AT_LEAST(1, 6)
82b75251 74 param.eOutputColorFormat = videoFormatI420;
d825b1a5 75#endif
82b75251
MS
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");
36b380dc 81 return AVERROR_UNKNOWN;
82b75251
MS
82 }
83
84 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
85
36b380dc 86 return 0;
82b75251
MS
87}
88
82b75251
MS
89static 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 linesize[3];
96 AVFrame *avframe = data;
82b75251 97 DECODING_STATE state;
83678dbb
MS
98#if OPENH264_VER_AT_LEAST(1, 7)
99 int opt;
100#endif
82b75251 101
e1e3a122
MS
102 if (!avpkt->data) {
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);
107#else
108 return 0;
109#endif
110 } else {
111 info.uiInBsTimeStamp = avpkt->pts;
eec93e57
MS
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
116 // B-frames.
117 state = (*s->decoder)->DecodeFrameNoDelay(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
118#else
e1e3a122 119 state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
eec93e57 120#endif
e1e3a122 121 }
030de53e 122 if (state != dsErrorFree) {
eec93e57 123 av_log(avctx, AV_LOG_ERROR, "DecodeFrame failed\n");
030de53e
MS
124 return AVERROR_UNKNOWN;
125 }
126 if (info.iBufferStatus != 1) {
127 av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
128 return avpkt->size;
82b75251
MS
129 }
130
030de53e
MS
131 ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
132 // The decoder doesn't (currently) support decoding into a user
133 // provided buffer, so do a copy instead.
134 if (ff_get_buffer(avctx, avframe, 0) < 0) {
135 av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
136 return AVERROR(ENOMEM);
137 }
138
139 linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
140 linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
141 av_image_copy(avframe->data, avframe->linesize, (const uint8_t **) ptrs, linesize, avctx->pix_fmt, avctx->width, avctx->height);
142
e1e3a122
MS
143 avframe->pts = info.uiOutYuvTimeStamp;
144 avframe->pkt_dts = AV_NOPTS_VALUE;
82b75251
MS
145#if FF_API_PKT_PTS
146FF_DISABLE_DEPRECATION_WARNINGS
030de53e 147 avframe->pkt_pts = avpkt->pts;
82b75251
MS
148FF_ENABLE_DEPRECATION_WARNINGS
149#endif
83678dbb
MS
150#if OPENH264_VER_AT_LEAST(1, 7)
151 (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_PROFILE, &opt);
152 avctx->profile = opt;
153 (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_LEVEL, &opt);
154 avctx->level = opt;
155#endif
82b75251 156
030de53e 157 *got_frame = 1;
82b75251
MS
158 return avpkt->size;
159}
160
161AVCodec ff_libopenh264_decoder = {
162 .name = "libopenh264",
163 .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
164 .type = AVMEDIA_TYPE_VIDEO,
165 .id = AV_CODEC_ID_H264,
166 .priv_data_size = sizeof(SVCContext),
167 .init = svc_decode_init,
168 .decode = svc_decode_frame,
169 .close = svc_decode_close,
82b75251
MS
170 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
171 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_THREADSAFE |
172 FF_CODEC_CAP_INIT_CLEANUP,
030de53e 173 .bsfs = "h264_mp4toannexb",
47687a2f 174 .wrapper_name = "libopenh264",
82b75251 175};