Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 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" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 33 | #include "codec_internal.h" |
Andreas Rheinhardt | 66b691f | 2022-08-24 19:28:16 | [diff] [blame] | 34 | #include "decode.h" |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 35 | #include "libopenh264.h" |
| 36 | |
| 37 | typedef struct SVCContext { |
| 38 | ISVCDecoder *decoder; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 39 | } SVCContext; |
| 40 | |
| 41 | static av_cold int svc_decode_close(AVCodecContext *avctx) |
| 42 | { |
| 43 | SVCContext *s = avctx->priv_data; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 44 | |
| 45 | if (s->decoder) |
| 46 | WelsDestroyDecoder(s->decoder); |
| 47 | |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static av_cold int svc_decode_init(AVCodecContext *avctx) |
| 52 | { |
| 53 | SVCContext *s = avctx->priv_data; |
| 54 | SDecodingParam param = { 0 }; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 55 | int log_level; |
| 56 | WelsTraceCallback callback_function; |
| 57 | |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 58 | if (WelsCreateDecoder(&s->decoder)) { |
| 59 | av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n"); |
Martin Storsjö | 36b380d | 2016-07-14 19:20:25 | [diff] [blame] | 60 | return AVERROR_UNKNOWN; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 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 | |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 70 | #if !OPENH264_VER_AT_LEAST(1, 6) |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 71 | param.eOutputColorFormat = videoFormatI420; |
Martin Storsjö | 293676c | 2016-07-08 20:21:41 | [diff] [blame] | 72 | #endif |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 73 | param.eEcActiveIdc = ERROR_CON_DISABLE; |
| 74 | param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; |
| 75 | |
| 76 | if ((*s->decoder)->Initialize(s->decoder, ¶m) != cmResultSuccess) { |
| 77 | av_log(avctx, AV_LOG_ERROR, "Initialize failed\n"); |
Martin Storsjö | 36b380d | 2016-07-14 19:20:25 | [diff] [blame] | 78 | return AVERROR_UNKNOWN; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
| 82 | |
Martin Storsjö | 36b380d | 2016-07-14 19:20:25 | [diff] [blame] | 83 | return 0; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 84 | } |
| 85 | |
Andreas Rheinhardt | ce7dbd0 | 2022-03-30 19:33:24 | [diff] [blame] | 86 | static int svc_decode_frame(AVCodecContext *avctx, AVFrame *avframe, |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 87 | int *got_frame, AVPacket *avpkt) |
| 88 | { |
| 89 | SVCContext *s = avctx->priv_data; |
| 90 | SBufferInfo info = { 0 }; |
Andreas Rheinhardt | b0b9010 | 2021-12-06 11:20:21 | [diff] [blame] | 91 | uint8_t *ptrs[4] = { NULL }; |
| 92 | int ret, linesize[4]; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 93 | DECODING_STATE state; |
Martin Storsjö | 83678db | 2018-08-31 09:25:40 | [diff] [blame] | 94 | #if OPENH264_VER_AT_LEAST(1, 7) |
| 95 | int opt; |
| 96 | #endif |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 97 | |
Martin Storsjö | e1e3a12 | 2018-07-30 11:58:37 | [diff] [blame] | 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; |
Martin Storsjö | eec93e5 | 2019-01-25 08:28:46 | [diff] [blame] | 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 |
Martin Storsjö | e1e3a12 | 2018-07-30 11:58:37 | [diff] [blame] | 115 | state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info); |
Martin Storsjö | eec93e5 | 2019-01-25 08:28:46 | [diff] [blame] | 116 | #endif |
Martin Storsjö | e1e3a12 | 2018-07-30 11:58:37 | [diff] [blame] | 117 | } |
Martin Storsjö | 030de53 | 2017-02-15 09:06:17 | [diff] [blame] | 118 | if (state != dsErrorFree) { |
Martin Storsjö | eec93e5 | 2019-01-25 08:28:46 | [diff] [blame] | 119 | av_log(avctx, AV_LOG_ERROR, "DecodeFrame failed\n"); |
Martin Storsjö | 030de53 | 2017-02-15 09:06:17 | [diff] [blame] | 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; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 125 | } |
| 126 | |
James Almer | 93dfc4f | 2017-09-28 03:49:05 | [diff] [blame] | 127 | ret = ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight); |
| 128 | if (ret < 0) |
| 129 | return ret; |
Martin Storsjö | 030de53 | 2017-02-15 09:06:17 | [diff] [blame] | 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 | } |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 136 | |
Martin Storsjö | 030de53 | 2017-02-15 09:06:17 | [diff] [blame] | 137 | linesize[0] = info.UsrData.sSystemBuffer.iStride[0]; |
| 138 | linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1]; |
Andreas Rheinhardt | b0b9010 | 2021-12-06 11:20:21 | [diff] [blame] | 139 | linesize[3] = 0; |
Andreas Rheinhardt | 423b6a7 | 2023-09-06 22:09:10 | [diff] [blame] | 140 | av_image_copy2(avframe->data, avframe->linesize, ptrs, linesize, |
| 141 | avctx->pix_fmt, avctx->width, avctx->height); |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 142 | |
Martin Storsjö | e1e3a12 | 2018-07-30 11:58:37 | [diff] [blame] | 143 | avframe->pts = info.uiOutYuvTimeStamp; |
| 144 | avframe->pkt_dts = AV_NOPTS_VALUE; |
Martin Storsjö | 83678db | 2018-08-31 09:25:40 | [diff] [blame] | 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 |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 151 | |
Martin Storsjö | 030de53 | 2017-02-15 09:06:17 | [diff] [blame] | 152 | *got_frame = 1; |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 153 | return avpkt->size; |
| 154 | } |
| 155 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 156 | const FFCodec ff_libopenh264_decoder = { |
| 157 | .p.name = "libopenh264", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame] | 158 | CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 159 | .p.type = AVMEDIA_TYPE_VIDEO, |
| 160 | .p.id = AV_CODEC_ID_H264, |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 161 | .priv_data_size = sizeof(SVCContext), |
| 162 | .init = svc_decode_init, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 163 | FF_CODEC_DECODE_CB(svc_decode_frame), |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 164 | .close = svc_decode_close, |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 165 | .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1, |
Andreas Rheinhardt | 21b23ce | 2022-07-09 22:05:45 | [diff] [blame] | 166 | .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 167 | FF_CODEC_CAP_INIT_CLEANUP, |
Martin Storsjö | 030de53 | 2017-02-15 09:06:17 | [diff] [blame] | 168 | .bsfs = "h264_mp4toannexb", |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 169 | .p.wrapper_name = "libopenh264", |
Martin Storsjö | c5d326f | 2016-06-23 21:58:17 | [diff] [blame] | 170 | }; |