FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
34 #include "libopenh264.h"
35 
36 typedef struct SVCContext {
37  ISVCDecoder *decoder;
41 } SVCContext;
42 
44 {
45  SVCContext *s = avctx->priv_data;
46  AVPacket pkt;
47 
48  if (s->decoder)
49  WelsDestroyDecoder(s->decoder);
50 
51  while (s->packet_fifo && av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
52  av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
53  av_packet_unref(&pkt);
54  }
55 
56  av_bsf_free(&s->bsf);
59 
60  return 0;
61 }
62 
64 {
65  SVCContext *s = avctx->priv_data;
66  SDecodingParam param = { 0 };
67  int err;
68  int log_level;
69  WelsTraceCallback callback_function;
70 
71  if ((err = ff_libopenh264_check_version(avctx)) < 0)
72  return err;
73 
74  s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
75  if (!s->packet_fifo) {
76  err = AVERROR(ENOMEM);
77  goto fail;
78  }
79 
80  if (WelsCreateDecoder(&s->decoder)) {
81  av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
82  err = AVERROR_UNKNOWN;
83  goto fail;
84  }
85 
86  // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
87  log_level = WELS_LOG_DETAIL;
88  callback_function = ff_libopenh264_trace_callback;
89  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
90  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
91  (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
92 
93 #if !OPENH264_VER_AT_LEAST(1, 6)
94  param.eOutputColorFormat = videoFormatI420;
95 #endif
96  param.eEcActiveIdc = ERROR_CON_DISABLE;
97  param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
98 
99  if ((*s->decoder)->Initialize(s->decoder, &param) != cmResultSuccess) {
100  av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
101  err = AVERROR_UNKNOWN;
102  goto fail;
103  }
104 
105  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
106 
107 fail:
108  return err;
109 }
110 
111 static int init_bsf(AVCodecContext *avctx)
112 {
113  SVCContext *s = avctx->priv_data;
114  const AVBitStreamFilter *filter;
115  int ret;
116 
117  if (s->bsf)
118  return 0;
119 
120  // If the input stream already is annex b, this BSF only passes the
121  // packets through unchanged.
122  filter = av_bsf_get_by_name("h264_mp4toannexb");
123  if (!filter)
124  return AVERROR_BUG;
125 
126  ret = av_bsf_alloc(filter, &s->bsf);
127  if (ret < 0)
128  return ret;
129 
130  ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
131  if (ret < 0)
132  return ret;
133 
134  s->bsf->time_base_in = avctx->time_base;
135 
136  ret = av_bsf_init(s->bsf);
137  if (ret < 0)
138  return ret;
139 
140  return ret;
141 }
142 
143 static int svc_decode_frame(AVCodecContext *avctx, void *data,
144  int *got_frame, AVPacket *avpkt)
145 {
146  SVCContext *s = avctx->priv_data;
147  SBufferInfo info = { 0 };
148  uint8_t* ptrs[3];
149  int linesize[3];
150  AVFrame *avframe = data;
151  int ret;
152  DECODING_STATE state;
153 
154  if ((ret = init_bsf(avctx)) < 0)
155  return ret;
156 
157  if (avpkt->size) {
158  AVPacket input_ref = { 0 };
159  if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
160  ret = av_fifo_realloc2(s->packet_fifo,
161  av_fifo_size(s->packet_fifo) + sizeof(input_ref));
162  if (ret < 0)
163  return ret;
164  }
165 
166  ret = av_packet_ref(&input_ref, avpkt);
167  if (ret < 0)
168  return ret;
169  av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
170  }
171 
172  while (!*got_frame) {
173  /* prepare the input data -- convert to Annex B if needed */
174  if (s->pkt_filtered.size <= 0) {
175  AVPacket input_ref;
176 
177  /* no more data */
178  if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
179  return avpkt->size ? avpkt->size : 0;
180 
182 
183  av_fifo_generic_read(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
184  ret = av_bsf_send_packet(s->bsf, &input_ref);
185  if (ret < 0) {
186  av_packet_unref(&input_ref);
187  return ret;
188  }
189 
190  ret = av_bsf_receive_packet(s->bsf, &s->pkt_filtered);
191  if (ret < 0)
192  av_packet_move_ref(&s->pkt_filtered, &input_ref);
193  else
194  av_packet_unref(&input_ref);
195  }
196 
197  state = (*s->decoder)->DecodeFrame2(s->decoder, s->pkt_filtered.data, s->pkt_filtered.size, ptrs, &info);
198  s->pkt_filtered.size = 0;
199  if (state != dsErrorFree) {
200  av_log(avctx, AV_LOG_ERROR, "DecodeFrame2 failed\n");
201  return AVERROR_UNKNOWN;
202  }
203  if (info.iBufferStatus != 1) {
204  av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
205  continue;
206  }
207 
208  ret = ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
209  if (ret < 0)
210  return ret;
211  // The decoder doesn't (currently) support decoding into a user
212  // provided buffer, so do a copy instead.
213  if (ff_get_buffer(avctx, avframe, 0) < 0) {
214  av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
215  return AVERROR(ENOMEM);
216  }
217 
218  linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
219  linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
220  av_image_copy(avframe->data, avframe->linesize, (const uint8_t **) ptrs, linesize, avctx->pix_fmt, avctx->width, avctx->height);
221 
222  avframe->pts = s->pkt_filtered.pts;
223  avframe->pkt_dts = s->pkt_filtered.dts;
224 #if FF_API_PKT_PTS
226  avframe->pkt_pts = s->pkt_filtered.pts;
228 #endif
229 
230  *got_frame = 1;
231  }
232  return avpkt->size;
233 }
234 
236  .name = "libopenh264",
237  .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
238  .type = AVMEDIA_TYPE_VIDEO,
239  .id = AV_CODEC_ID_H264,
240  .priv_data_size = sizeof(SVCContext),
243  .close = svc_decode_close,
244  // The decoder doesn't currently support B-frames, and the decoder's API
245  // doesn't support reordering/delay, but the BSF could incur delay.
246  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
249 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
void ff_libopenh264_trace_callback(void *ctx, int level, const char *msg)
Definition: libopenh264.c:41
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:36
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static int init_bsf(AVCodecContext *avctx)
AVBSFContext * bsf
misc image utilities
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:210
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
The bitstream filter state.
Definition: avcodec.h:5731
int size
Definition: avcodec.h:1602
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3600
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:135
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:122
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1813
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:82
#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: avcodec.h:984
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:199
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
static av_cold int svc_decode_close(AVCodecContext *avctx)
uint8_t
#define av_cold
Definition: attributes.h:82
static int svc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
Definition: fifo.c:82
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:80
uint8_t * data
Definition: avcodec.h:1601
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:622
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:55
#define av_log(a,...)
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:576
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static av_cold int svc_decode_init(AVCodecContext *avctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:213
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: avcodec.h:5768
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
#define fail()
Definition: checkasm.h:83
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:302
AVPacket pkt_filtered
int width
picture width / height.
Definition: avcodec.h:1863
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:176
Libavcodec external API header.
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:77
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
main external API structure.
Definition: avcodec.h:1676
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:567
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:947
static struct @246 state
a very simple circular buffer FIFO implementation
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:4166
AVFifoBuffer * packet_fifo
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: internal.h:55
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:276
AVCodec ff_libopenh264_decoder
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:284
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
common internal api header.
common internal and external API header
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void * priv_data
Definition: avcodec.h:1718
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
int ff_libopenh264_check_version(void *logctx)
Definition: libopenh264.c:49
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1600
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2035
ISVCDecoder * decoder
This structure stores compressed data.
Definition: avcodec.h:1578
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5757
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1594