FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mediacodecdec_h264.c
Go to the documentation of this file.
1 /*
2  * Android MediaCodec H.264 decoder
3  *
4  * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <stdint.h>
24 #include <string.h>
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/common.h"
28 #include "libavutil/fifo.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/pixfmt.h"
32 #include "libavutil/atomic.h"
33 
34 #include "avcodec.h"
35 #include "h264_parse.h"
36 #include "internal.h"
37 #include "mediacodecdec.h"
38 #include "mediacodec_wrapper.h"
39 
40 #define CODEC_MIME "video/avc"
41 
42 typedef struct MediaCodecH264DecContext {
43 
45 
47 
49 
51 
53 
55 {
57 
58  ff_mediacodec_dec_close(avctx, s->ctx);
59  s->ctx = NULL;
60 
61  av_fifo_free(s->fifo);
62 
63  av_bsf_free(&s->bsf);
65 
66  return 0;
67 }
68 
69 static int h264_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
70 {
71  int i;
72  int ret = 0;
73  uint8_t *p = NULL;
74  static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
75 
76  if (!out || !out_size) {
77  return AVERROR(EINVAL);
78  }
79 
80  p = av_malloc(sizeof(nalu_header) + src_size);
81  if (!p) {
82  return AVERROR(ENOMEM);
83  }
84 
85  *out = p;
86  *out_size = sizeof(nalu_header) + src_size;
87 
88  memcpy(p, nalu_header, sizeof(nalu_header));
89  memcpy(p + sizeof(nalu_header), src, src_size);
90 
91  /* Escape 0x00, 0x00, 0x0{0-3} pattern */
92  for (i = 4; i < *out_size; i++) {
93  if (i < *out_size - 3 &&
94  p[i + 0] == 0 &&
95  p[i + 1] == 0 &&
96  p[i + 2] <= 3) {
97  uint8_t *new;
98 
99  *out_size += 1;
100  new = av_realloc(*out, *out_size);
101  if (!new) {
102  ret = AVERROR(ENOMEM);
103  goto done;
104  }
105  *out = p = new;
106 
107  i = i + 2;
108  memmove(p + i + 1, p + i, *out_size - (i + 1));
109  p[i] = 0x03;
110  }
111  }
112 done:
113  if (ret < 0) {
114  av_freep(out);
115  *out_size = 0;
116  }
117 
118  return ret;
119 }
120 
122 {
123  int i;
124  int ret;
125 
126  H264ParamSets ps;
127  const PPS *pps = NULL;
128  const SPS *sps = NULL;
129  int is_avc = 0;
130  int nal_length_size = 0;
131 
132  const AVBitStreamFilter *bsf = NULL;
133 
136 
137  memset(&ps, 0, sizeof(ps));
138 
139  format = ff_AMediaFormat_new();
140  if (!format) {
141  av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
142  ret = AVERROR_EXTERNAL;
143  goto done;
144  }
145 
146  ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
147  ff_AMediaFormat_setInt32(format, "width", avctx->width);
148  ff_AMediaFormat_setInt32(format, "height", avctx->height);
149 
151  &ps, &is_avc, &nal_length_size, 0, avctx);
152  if (ret < 0) {
153  goto done;
154  }
155 
156  for (i = 0; i < MAX_PPS_COUNT; i++) {
157  if (ps.pps_list[i]) {
158  pps = (const PPS*)ps.pps_list[i]->data;
159  break;
160  }
161  }
162 
163  if (pps) {
164  if (ps.sps_list[pps->sps_id]) {
165  sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
166  }
167  }
168 
169  if (pps && sps) {
170  uint8_t *data = NULL;
171  size_t data_size = 0;
172 
173  if ((ret = h264_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
174  goto done;
175  }
176  ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
177  av_freep(&data);
178 
179  if ((ret = h264_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
180  goto done;
181  }
182  ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
183  av_freep(&data);
184  } else {
185  av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
186  ret = AVERROR_INVALIDDATA;
187  goto done;
188  }
189 
190  s->ctx = av_mallocz(sizeof(*s->ctx));
191  if (!s->ctx) {
192  av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
193  ret = AVERROR(ENOMEM);
194  goto done;
195  }
196 
197  if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, CODEC_MIME, format)) < 0) {
198  s->ctx = NULL;
199  goto done;
200  }
201 
202  av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
203 
204  s->fifo = av_fifo_alloc(sizeof(AVPacket));
205  if (!s->fifo) {
206  ret = AVERROR(ENOMEM);
207  goto done;
208  }
209 
210  bsf = av_bsf_get_by_name("h264_mp4toannexb");
211  if(!bsf) {
212  ret = AVERROR_BSF_NOT_FOUND;
213  goto done;
214  }
215 
216  if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
217  goto done;
218  }
219 
220  if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
221  ((ret = av_bsf_init(s->bsf)) < 0)) {
222  goto done;
223  }
224 
226 
227 done:
228  if (format) {
229  ff_AMediaFormat_delete(format);
230  }
231 
232  if (ret < 0) {
234  }
235 
236  ff_h264_ps_uninit(&ps);
237 
238  return ret;
239 }
240 
241 
243  int *got_frame, AVPacket *pkt)
244 {
246 
247  return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
248 }
249 
250 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
251  int *got_frame, AVPacket *avpkt)
252 {
254  AVFrame *frame = data;
255  int ret;
256 
257  /* buffer the input packet */
258  if (avpkt->size) {
259  AVPacket input_pkt = { 0 };
260 
261  if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
262  ret = av_fifo_realloc2(s->fifo,
263  av_fifo_size(s->fifo) + sizeof(input_pkt));
264  if (ret < 0)
265  return ret;
266  }
267 
268  ret = av_packet_ref(&input_pkt, avpkt);
269  if (ret < 0)
270  return ret;
271  av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
272  }
273 
274  /*
275  * MediaCodec.flush() discards both input and output buffers, thus we
276  * need to delay the call to this function until the user has released or
277  * renderered the frames he retains.
278  *
279  * After we have buffered an input packet, check if the codec is in the
280  * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
281  *
282  * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
283  * the codec (because the user retains frames). The codec stays in the
284  * flushing state.
285  *
286  * ff_mediacodec_dec_flush returns 1 if the flush can actually be
287  * performed on the codec. The codec leaves the flushing state and can
288  * process again packets.
289  *
290  * ff_mediacodec_dec_flush returns a negative value if an error has
291  * occurred.
292  *
293  */
294  if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
295  if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
296  return avpkt->size;
297  }
298  }
299 
300  /* process buffered data */
301  while (!*got_frame) {
302  /* prepare the input data -- convert to Annex B if needed */
303  if (s->filtered_pkt.size <= 0) {
304  AVPacket input_pkt = { 0 };
305 
307 
308  /* no more data */
309  if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
310  return avpkt->size ? avpkt->size :
311  ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
312  }
313 
314  av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
315 
316  ret = av_bsf_send_packet(s->bsf, &input_pkt);
317  if (ret < 0) {
318  return ret;
319  }
320 
321  ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
322  if (ret == AVERROR(EAGAIN)) {
323  goto done;
324  }
325 
326  /* h264_mp4toannexb is used here and does not requires flushing */
327  av_assert0(ret != AVERROR_EOF);
328 
329  if (ret < 0) {
330  return ret;
331  }
332  }
333 
334  ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
335  if (ret < 0)
336  return ret;
337 
338  s->filtered_pkt.size -= ret;
339  s->filtered_pkt.data += ret;
340  }
341 done:
342  return avpkt->size;
343 }
344 
346 {
348 
349  while (av_fifo_size(s->fifo)) {
350  AVPacket pkt;
351  av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
352  av_packet_unref(&pkt);
353  }
354  av_fifo_reset(s->fifo);
355 
357 
358  ff_mediacodec_dec_flush(avctx, s->ctx);
359 }
360 
362  .name = "h264_mediacodec",
363  .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
364  .type = AVMEDIA_TYPE_VIDEO,
365  .id = AV_CODEC_ID_H264,
366  .priv_data_size = sizeof(MediaCodecH264DecContext),
370  .close = mediacodec_decode_close,
371  .capabilities = CODEC_CAP_DELAY,
372  .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
373 };
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
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int ff_mediacodec_dec_is_flushing(AVCodecContext *avctx, MediaCodecDecContext *s)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:145
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: h264_ps.h:137
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
MediaCodecDecContext * ctx
Sequence parameter set.
Definition: h264_ps.h:43
The bitstream filter state.
Definition: avcodec.h:5711
int size
Definition: avcodec.h:1591
void ff_AMediaFormat_setBuffer(FFAMediaFormat *format, const char *name, void *data, size_t size)
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
Picture parameter set.
Definition: h264_ps.h:107
int out_size
Definition: movenc.c:55
#define CODEC_MIME
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3573
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
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:82
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:199
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVOptions.
FFAMediaFormat * ff_AMediaFormat_new(void)
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
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1774
static AVFrame * frame
static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
int ff_h264_decode_extradata(const uint8_t *data, int size, H264ParamSets *ps, int *is_avc, int *nal_length_size, int err_recognition, void *logctx)
Definition: h264_parse.c:422
#define MAX_PPS_COUNT
Definition: h264_ps.h:38
uint8_t * data
Definition: avcodec.h:1590
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:575
AVCodec ff_h264_mediacodec_decoder
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int mediacodec_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define 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:1165
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
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
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3580
static int h264_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s, AVFrame *frame, int *got_frame, AVPacket *pkt)
size_t data_size
Definition: h264_ps.h:101
uint8_t data[4096]
Definition: h264_ps.h:127
void ff_AMediaFormat_setInt32(FFAMediaFormat *format, const char *name, int32_t value)
int width
picture width / height.
Definition: avcodec.h:1846
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:176
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
Definition: error.h:49
int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
#define src
Definition: vp9dsp.c:530
void ff_AMediaFormat_setString(FFAMediaFormat *format, const char *name, const char *value)
static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
AVBufferRef * pps_list[MAX_PPS_COUNT]
Definition: h264_ps.h:138
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 av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
Definition: fifo.c:87
main external API structure.
Definition: avcodec.h:1659
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:566
uint8_t * data
The data buffer.
Definition: buffer.h:89
a very simple circular buffer FIFO implementation
int extradata_size
Definition: avcodec.h:1775
static const char * format
Definition: movenc.c:47
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:4087
unsigned int sps_id
Definition: h264_ps.h:108
int ff_AMediaFormat_delete(FFAMediaFormat *format)
#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
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:750
common internal api header.
common internal and external API header
uint8_t data[4096]
Definition: h264_ps.h:100
size_t data_size
Definition: h264_ps.h:128
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
void ff_h264_ps_uninit(H264ParamSets *ps)
Uninit H264 param sets structure.
Definition: h264_ps.c:307
void * priv_data
Definition: avcodec.h:1701
pixel format definitions
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
static void mediacodec_decode_flush(AVCodecContext *avctx)
static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
FILE * out
Definition: movenc.c:54
#define av_freep(p)
int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, const char *mime, FFAMediaFormat *format)
void av_fifo_reset(AVFifoBuffer *f)
Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied...
Definition: fifo.c:71
H.264 decoder/parser shared code.
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1567
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5737