blob: b6a9bba2dc73dea3ebb3cd85ff00063b0f148e96 [file] [log] [blame]
Martin Storsjöc5d326f2016-06-23 21:58:171/*
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"
Andreas Rheinhardta688f3c2022-03-16 17:18:2833#include "codec_internal.h"
Andreas Rheinhardt66b691f2022-08-24 19:28:1634#include "decode.h"
Martin Storsjöc5d326f2016-06-23 21:58:1735#include "libopenh264.h"
36
37typedef struct SVCContext {
38 ISVCDecoder *decoder;
Martin Storsjöc5d326f2016-06-23 21:58:1739} SVCContext;
40
41static av_cold int svc_decode_close(AVCodecContext *avctx)
42{
43 SVCContext *s = avctx->priv_data;
Martin Storsjöc5d326f2016-06-23 21:58:1744
45 if (s->decoder)
46 WelsDestroyDecoder(s->decoder);
47
Martin Storsjöc5d326f2016-06-23 21:58:1748 return 0;
49}
50
51static av_cold int svc_decode_init(AVCodecContext *avctx)
52{
53 SVCContext *s = avctx->priv_data;
54 SDecodingParam param = { 0 };
Martin Storsjöc5d326f2016-06-23 21:58:1755 int log_level;
56 WelsTraceCallback callback_function;
57
Martin Storsjöc5d326f2016-06-23 21:58:1758 if (WelsCreateDecoder(&s->decoder)) {
59 av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
Martin Storsjö36b380d2016-07-14 19:20:2560 return AVERROR_UNKNOWN;
Martin Storsjöc5d326f2016-06-23 21:58:1761 }
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
Martin Storsjö293676c2016-07-08 20:21:4170#if !OPENH264_VER_AT_LEAST(1, 6)
Martin Storsjöc5d326f2016-06-23 21:58:1771 param.eOutputColorFormat = videoFormatI420;
Martin Storsjö293676c2016-07-08 20:21:4172#endif
Martin Storsjöc5d326f2016-06-23 21:58:1773 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");
Martin Storsjö36b380d2016-07-14 19:20:2578 return AVERROR_UNKNOWN;
Martin Storsjöc5d326f2016-06-23 21:58:1779 }
80
81 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
82
Martin Storsjö36b380d2016-07-14 19:20:2583 return 0;
Martin Storsjöc5d326f2016-06-23 21:58:1784}
85
Andreas Rheinhardtce7dbd02022-03-30 19:33:2486static int svc_decode_frame(AVCodecContext *avctx, AVFrame *avframe,
Martin Storsjöc5d326f2016-06-23 21:58:1787 int *got_frame, AVPacket *avpkt)
88{
89 SVCContext *s = avctx->priv_data;
90 SBufferInfo info = { 0 };
Andreas Rheinhardtb0b90102021-12-06 11:20:2191 uint8_t *ptrs[4] = { NULL };
92 int ret, linesize[4];
Martin Storsjöc5d326f2016-06-23 21:58:1793 DECODING_STATE state;
Martin Storsjö83678db2018-08-31 09:25:4094#if OPENH264_VER_AT_LEAST(1, 7)
95 int opt;
96#endif
Martin Storsjöc5d326f2016-06-23 21:58:1797
Martin Storsjöe1e3a122018-07-30 11:58:3798 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;
Martin Storsjöeec93e52019-01-25 08:28:46108#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
Martin Storsjöe1e3a122018-07-30 11:58:37115 state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
Martin Storsjöeec93e52019-01-25 08:28:46116#endif
Martin Storsjöe1e3a122018-07-30 11:58:37117 }
Martin Storsjö030de532017-02-15 09:06:17118 if (state != dsErrorFree) {
Martin Storsjöeec93e52019-01-25 08:28:46119 av_log(avctx, AV_LOG_ERROR, "DecodeFrame failed\n");
Martin Storsjö030de532017-02-15 09:06:17120 return AVERROR_UNKNOWN;
121 }
122 if (info.iBufferStatus != 1) {
123 av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
124 return avpkt->size;
Martin Storsjöc5d326f2016-06-23 21:58:17125 }
126
James Almer93dfc4f2017-09-28 03:49:05127 ret = ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
128 if (ret < 0)
129 return ret;
Martin Storsjö030de532017-02-15 09:06:17130 // 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 }
Martin Storsjöc5d326f2016-06-23 21:58:17136
Martin Storsjö030de532017-02-15 09:06:17137 linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
138 linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
Andreas Rheinhardtb0b90102021-12-06 11:20:21139 linesize[3] = 0;
Andreas Rheinhardt423b6a72023-09-06 22:09:10140 av_image_copy2(avframe->data, avframe->linesize, ptrs, linesize,
141 avctx->pix_fmt, avctx->width, avctx->height);
Martin Storsjöc5d326f2016-06-23 21:58:17142
Martin Storsjöe1e3a122018-07-30 11:58:37143 avframe->pts = info.uiOutYuvTimeStamp;
144 avframe->pkt_dts = AV_NOPTS_VALUE;
Martin Storsjö83678db2018-08-31 09:25:40145#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
Martin Storsjöc5d326f2016-06-23 21:58:17151
Martin Storsjö030de532017-02-15 09:06:17152 *got_frame = 1;
Martin Storsjöc5d326f2016-06-23 21:58:17153 return avpkt->size;
154}
155
Andreas Rheinhardt20f97272022-03-16 20:09:54156const FFCodec ff_libopenh264_decoder = {
157 .p.name = "libopenh264",
Andreas Rheinhardt48286d42022-08-29 11:38:02158 CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
Andreas Rheinhardt20f97272022-03-16 20:09:54159 .p.type = AVMEDIA_TYPE_VIDEO,
160 .p.id = AV_CODEC_ID_H264,
Martin Storsjöc5d326f2016-06-23 21:58:17161 .priv_data_size = sizeof(SVCContext),
162 .init = svc_decode_init,
Andreas Rheinhardt4243da42022-03-30 21:28:24163 FF_CODEC_DECODE_CB(svc_decode_frame),
Martin Storsjöc5d326f2016-06-23 21:58:17164 .close = svc_decode_close,
Andreas Rheinhardt20f97272022-03-16 20:09:54165 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
Andreas Rheinhardt21b23ce2022-07-09 22:05:45166 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS |
Martin Storsjöc5d326f2016-06-23 21:58:17167 FF_CODEC_CAP_INIT_CLEANUP,
Martin Storsjö030de532017-02-15 09:06:17168 .bsfs = "h264_mp4toannexb",
Andreas Rheinhardt20f97272022-03-16 20:09:54169 .p.wrapper_name = "libopenh264",
Martin Storsjöc5d326f2016-06-23 21:58:17170};