Mercurial > libavcodec.hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
11856:9d81bd6f2a81 | 11857:e1e986bb64d0 |
---|---|
23 #include "avcodec.h" | 23 #include "avcodec.h" |
24 | 24 |
25 typedef struct H264BSFContext { | 25 typedef struct H264BSFContext { |
26 uint8_t length_size; | 26 uint8_t length_size; |
27 uint8_t first_idr; | 27 uint8_t first_idr; |
28 uint8_t *sps_pps_data; | 28 int extradata_parsed; |
29 uint32_t size; | |
30 } H264BSFContext; | 29 } H264BSFContext; |
31 | 30 |
32 static void alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size, | 31 static void alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size, |
33 const uint8_t *sps_pps, uint32_t sps_pps_size, | 32 const uint8_t *sps_pps, uint32_t sps_pps_size, |
34 const uint8_t *in, uint32_t in_size) { | 33 const uint8_t *in, uint32_t in_size) { |
65 *poutbuf_size = buf_size; | 64 *poutbuf_size = buf_size; |
66 return 0; | 65 return 0; |
67 } | 66 } |
68 | 67 |
69 /* retrieve sps and pps NAL units from extradata */ | 68 /* retrieve sps and pps NAL units from extradata */ |
70 if (!ctx->sps_pps_data) { | 69 if (!ctx->extradata_parsed) { |
71 uint16_t unit_size; | 70 uint16_t unit_size; |
72 uint32_t total_size = 0; | 71 uint64_t total_size = 0; |
73 uint8_t *out = NULL, unit_nb, sps_done = 0; | 72 uint8_t *out = NULL, unit_nb, sps_done = 0; |
74 const uint8_t *extradata = avctx->extradata+4; | 73 const uint8_t *extradata = avctx->extradata+4; |
75 static const uint8_t nalu_header[4] = {0, 0, 0, 1}; | 74 static const uint8_t nalu_header[4] = {0, 0, 0, 1}; |
76 | 75 |
77 /* retrieve length coded size */ | 76 /* retrieve length coded size */ |
86 sps_done++; | 85 sps_done++; |
87 } | 86 } |
88 while (unit_nb--) { | 87 while (unit_nb--) { |
89 unit_size = AV_RB16(extradata); | 88 unit_size = AV_RB16(extradata); |
90 total_size += unit_size+4; | 89 total_size += unit_size+4; |
91 if (extradata+2+unit_size > avctx->extradata+avctx->extradata_size) { | 90 if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE || extradata+2+unit_size > avctx->extradata+avctx->extradata_size) { |
92 av_free(out); | 91 av_free(out); |
93 return AVERROR(EINVAL); | 92 return AVERROR(EINVAL); |
94 } | 93 } |
95 out = av_realloc(out, total_size); | 94 out = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE); |
96 if (!out) | 95 if (!out) |
97 return AVERROR(ENOMEM); | 96 return AVERROR(ENOMEM); |
98 memcpy(out+total_size-unit_size-4, nalu_header, 4); | 97 memcpy(out+total_size-unit_size-4, nalu_header, 4); |
99 memcpy(out+total_size-unit_size, extradata+2, unit_size); | 98 memcpy(out+total_size-unit_size, extradata+2, unit_size); |
100 extradata += 2+unit_size; | 99 extradata += 2+unit_size; |
101 | 100 |
102 if (!unit_nb && !sps_done++) | 101 if (!unit_nb && !sps_done++) |
103 unit_nb = *extradata++; /* number of pps unit(s) */ | 102 unit_nb = *extradata++; /* number of pps unit(s) */ |
104 } | 103 } |
105 | 104 |
106 ctx->sps_pps_data = out; | 105 memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
107 ctx->size = total_size; | 106 av_free(avctx->extradata); |
108 ctx->first_idr = 1; | 107 avctx->extradata = out; |
108 avctx->extradata_size = total_size; | |
109 ctx->first_idr = 1; | |
110 ctx->extradata_parsed = 1; | |
109 } | 111 } |
110 | 112 |
111 *poutbuf_size = 0; | 113 *poutbuf_size = 0; |
112 *poutbuf = NULL; | 114 *poutbuf = NULL; |
113 do { | 115 do { |
128 goto fail; | 130 goto fail; |
129 | 131 |
130 /* prepend only to the first type 5 NAL unit of an IDR picture */ | 132 /* prepend only to the first type 5 NAL unit of an IDR picture */ |
131 if (ctx->first_idr && unit_type == 5) { | 133 if (ctx->first_idr && unit_type == 5) { |
132 alloc_and_copy(poutbuf, poutbuf_size, | 134 alloc_and_copy(poutbuf, poutbuf_size, |
133 ctx->sps_pps_data, ctx->size, | 135 avctx->extradata, avctx->extradata_size, |
134 buf, nal_size); | 136 buf, nal_size); |
135 ctx->first_idr = 0; | 137 ctx->first_idr = 0; |
136 } | 138 } |
137 else { | 139 else { |
138 alloc_and_copy(poutbuf, poutbuf_size, | 140 alloc_and_copy(poutbuf, poutbuf_size, |
152 av_freep(poutbuf); | 154 av_freep(poutbuf); |
153 *poutbuf_size = 0; | 155 *poutbuf_size = 0; |
154 return AVERROR(EINVAL); | 156 return AVERROR(EINVAL); |
155 } | 157 } |
156 | 158 |
157 static void h264_mp4toannexb_close(AVBitStreamFilterContext *bsfc) | |
158 { | |
159 H264BSFContext *ctx = bsfc->priv_data; | |
160 av_freep(&ctx->sps_pps_data); | |
161 } | |
162 | |
163 AVBitStreamFilter h264_mp4toannexb_bsf = { | 159 AVBitStreamFilter h264_mp4toannexb_bsf = { |
164 "h264_mp4toannexb", | 160 "h264_mp4toannexb", |
165 sizeof(H264BSFContext), | 161 sizeof(H264BSFContext), |
166 h264_mp4toannexb_filter, | 162 h264_mp4toannexb_filter, |
167 h264_mp4toannexb_close, | |
168 }; | 163 }; |
169 | 164 |