Mercurial > libavcodec.hg
annotate h264_mp4toannexb_bsf.c @ 11857:e1e986bb64d0 libavcodec
Improve the mp4toannexb BSF to convert the extradata.
author | lucabe |
---|---|
date | Wed, 09 Jun 2010 06:47:01 +0000 |
parents | 9a4c9c165b3b |
children | fb5a6e98aa1c |
rev | line source |
---|---|
5644 | 1 /* |
9632 | 2 * H.264 MP4 to Annex B byte stream format filter |
8156 | 3 * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr> |
5644 | 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 | |
8573
2acf0ae7b041
Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents:
8156
diff
changeset
|
22 #include "libavutil/intreadwrite.h" |
5644 | 23 #include "avcodec.h" |
24 | |
25 typedef struct H264BSFContext { | |
26 uint8_t length_size; | |
27 uint8_t first_idr; | |
11857
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
28 int extradata_parsed; |
5644 | 29 } H264BSFContext; |
30 | |
31 static 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 { | |
6134
6d0c5a68d559
Fix a bug causing the generated stream to be corrupt if the buffer
andoma
parents:
5644
diff
changeset
|
45 (*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0; |
6d0c5a68d559
Fix a bug causing the generated stream to be corrupt if the buffer
andoma
parents:
5644
diff
changeset
|
46 (*poutbuf+offset+sps_pps_size)[2] = 1; |
5644 | 47 } |
48 } | |
49 | |
50 static 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; | |
11698
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
57 int32_t nal_size; |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
58 uint32_t cumul_size = 0; |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
59 const uint8_t *buf_end = buf + buf_size; |
5644 | 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 */ | |
11857
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
69 if (!ctx->extradata_parsed) { |
5644 | 70 uint16_t unit_size; |
11857
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
71 uint64_t total_size = 0; |
5644 | 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; | |
11857
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
90 if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE || extradata+2+unit_size > avctx->extradata+avctx->extradata_size) { |
5644 | 91 av_free(out); |
92 return AVERROR(EINVAL); | |
93 } | |
11857
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
94 out = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE); |
5644 | 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 | |
11857
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
105 memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
106 av_free(avctx->extradata); |
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
107 avctx->extradata = out; |
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
108 avctx->extradata_size = total_size; |
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
109 ctx->first_idr = 1; |
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
110 ctx->extradata_parsed = 1; |
5644 | 111 } |
112 | |
113 *poutbuf_size = 0; | |
114 *poutbuf = NULL; | |
115 do { | |
11698
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
116 if (buf + ctx->length_size > buf_end) |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
117 goto fail; |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
118 |
5644 | 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 | |
11698
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
129 if (buf + nal_size > buf_end || nal_size < 0) |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
130 goto fail; |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
131 |
5644 | 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, | |
11857
e1e986bb64d0
Improve the mp4toannexb BSF to convert the extradata.
lucabe
parents:
11698
diff
changeset
|
135 avctx->extradata, avctx->extradata_size, |
5644 | 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; | |
11698
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
152 |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
153 fail: |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
154 av_freep(poutbuf); |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
155 *poutbuf_size = 0; |
9a4c9c165b3b
Check NAL unit size to avoid reading past the buffer.
benoit
parents:
9632
diff
changeset
|
156 return AVERROR(EINVAL); |
5644 | 157 } |
158 | |
159 AVBitStreamFilter h264_mp4toannexb_bsf = { | |
160 "h264_mp4toannexb", | |
161 sizeof(H264BSFContext), | |
162 h264_mp4toannexb_filter, | |
163 }; | |
164 |