Make all the numeric options accept in input an expression which is
[libav.git] / libavcodec / h264_mp4toannexb_bsf.c
CommitLineData
bdfae2a5 1/*
ec21c215 2 * H.264 MP4 to Annex B byte stream format filter
074bfa7d 3 * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
bdfae2a5
BF
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
6a5d31ac 22#include "libavutil/intreadwrite.h"
bdfae2a5
BF
23#include "avcodec.h"
24
25typedef struct H264BSFContext {
26 uint8_t length_size;
27 uint8_t first_idr;
d5cc1ed7 28 int extradata_parsed;
bdfae2a5
BF
29} H264BSFContext;
30
31static void alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
32 const uint8_t *sps_pps, uint32_t sps_pps_size,
33 const uint8_t *in, uint32_t in_size) {
34 uint32_t offset = *poutbuf_size;
35 uint8_t nal_header_size = offset ? 3 : 4;
36
37 *poutbuf_size += sps_pps_size+in_size+nal_header_size;
38 *poutbuf = av_realloc(*poutbuf, *poutbuf_size);
39 if (sps_pps)
40 memcpy(*poutbuf+offset, sps_pps, sps_pps_size);
41 memcpy(*poutbuf+sps_pps_size+nal_header_size+offset, in, in_size);
42 if (!offset)
43 AV_WB32(*poutbuf+sps_pps_size, 1);
44 else {
48aecf5a
45 (*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0;
46 (*poutbuf+offset+sps_pps_size)[2] = 1;
bdfae2a5
BF
47 }
48}
49
50static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
51 AVCodecContext *avctx, const char *args,
52 uint8_t **poutbuf, int *poutbuf_size,
53 const uint8_t *buf, int buf_size,
54 int keyframe) {
55 H264BSFContext *ctx = bsfc->priv_data;
56 uint8_t unit_type;
52486603
TD
57 int32_t nal_size;
58 uint32_t cumul_size = 0;
59 const uint8_t *buf_end = buf + buf_size;
bdfae2a5
BF
60
61 /* nothing to filter */
62 if (!avctx->extradata || avctx->extradata_size < 6) {
63 *poutbuf = (uint8_t*) buf;
64 *poutbuf_size = buf_size;
65 return 0;
66 }
67
68 /* retrieve sps and pps NAL units from extradata */
d5cc1ed7 69 if (!ctx->extradata_parsed) {
bdfae2a5 70 uint16_t unit_size;
d5cc1ed7 71 uint64_t total_size = 0;
bdfae2a5
BF
72 uint8_t *out = NULL, unit_nb, sps_done = 0;
73 const uint8_t *extradata = avctx->extradata+4;
74 static const uint8_t nalu_header[4] = {0, 0, 0, 1};
75
76 /* retrieve length coded size */
77 ctx->length_size = (*extradata++ & 0x3) + 1;
78 if (ctx->length_size == 3)
79 return AVERROR(EINVAL);
80
81 /* retrieve sps and pps unit(s) */
82 unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
83 if (!unit_nb) {
84 unit_nb = *extradata++; /* number of pps unit(s) */
85 sps_done++;
86 }
87 while (unit_nb--) {
88 unit_size = AV_RB16(extradata);
89 total_size += unit_size+4;
d5cc1ed7 90 if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE || extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
bdfae2a5
BF
91 av_free(out);
92 return AVERROR(EINVAL);
93 }
d5cc1ed7 94 out = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE);
bdfae2a5
BF
95 if (!out)
96 return AVERROR(ENOMEM);
97 memcpy(out+total_size-unit_size-4, nalu_header, 4);
98 memcpy(out+total_size-unit_size, extradata+2, unit_size);
99 extradata += 2+unit_size;
100
101 if (!unit_nb && !sps_done++)
102 unit_nb = *extradata++; /* number of pps unit(s) */
103 }
104
d5cc1ed7
LA
105 memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
106 av_free(avctx->extradata);
107 avctx->extradata = out;
108 avctx->extradata_size = total_size;
109 ctx->first_idr = 1;
110 ctx->extradata_parsed = 1;
bdfae2a5
BF
111 }
112
113 *poutbuf_size = 0;
114 *poutbuf = NULL;
115 do {
52486603
TD
116 if (buf + ctx->length_size > buf_end)
117 goto fail;
118
bdfae2a5
BF
119 if (ctx->length_size == 1)
120 nal_size = buf[0];
121 else if (ctx->length_size == 2)
122 nal_size = AV_RB16(buf);
123 else
124 nal_size = AV_RB32(buf);
125
126 buf += ctx->length_size;
127 unit_type = *buf & 0x1f;
128
52486603
TD
129 if (buf + nal_size > buf_end || nal_size < 0)
130 goto fail;
131
bdfae2a5
BF
132 /* prepend only to the first type 5 NAL unit of an IDR picture */
133 if (ctx->first_idr && unit_type == 5) {
134 alloc_and_copy(poutbuf, poutbuf_size,
d5cc1ed7 135 avctx->extradata, avctx->extradata_size,
bdfae2a5
BF
136 buf, nal_size);
137 ctx->first_idr = 0;
138 }
139 else {
140 alloc_and_copy(poutbuf, poutbuf_size,
141 NULL, 0,
142 buf, nal_size);
143 if (!ctx->first_idr && unit_type == 1)
144 ctx->first_idr = 1;
145 }
146
147 buf += nal_size;
148 cumul_size += nal_size + ctx->length_size;
149 } while (cumul_size < buf_size);
150
151 return 1;
52486603
TD
152
153fail:
154 av_freep(poutbuf);
155 *poutbuf_size = 0;
156 return AVERROR(EINVAL);
bdfae2a5
BF
157}
158
bdfae2a5
BF
159AVBitStreamFilter h264_mp4toannexb_bsf = {
160 "h264_mp4toannexb",
161 sizeof(H264BSFContext),
162 h264_mp4toannexb_filter,
bdfae2a5
BF
163};
164