2 * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/intreadwrite.h"
24 typedef struct H264BSFContext
{
27 uint8_t *sps_pps_data
;
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;
37 *poutbuf_size
+= sps_pps_size
+in_size
+nal_header_size
;
38 *poutbuf
= av_realloc(*poutbuf
, *poutbuf_size
);
40 memcpy(*poutbuf
+offset
, sps_pps
, sps_pps_size
);
41 memcpy(*poutbuf
+sps_pps_size
+nal_header_size
+offset
, in
, in_size
);
43 AV_WB32(*poutbuf
+sps_pps_size
, 1);
45 (*poutbuf
+offset
+sps_pps_size
)[0] = (*poutbuf
+offset
+sps_pps_size
)[1] = 0;
46 (*poutbuf
+offset
+sps_pps_size
)[2] = 1;
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
,
55 H264BSFContext
*ctx
= bsfc
->priv_data
;
57 uint32_t nal_size
, cumul_size
= 0;
59 /* nothing to filter */
60 if (!avctx
->extradata
|| avctx
->extradata_size
< 6) {
61 *poutbuf
= (uint8_t*) buf
;
62 *poutbuf_size
= buf_size
;
66 /* retrieve sps and pps NAL units from extradata */
67 if (!ctx
->sps_pps_data
) {
69 uint32_t total_size
= 0;
70 uint8_t *out
= NULL
, unit_nb
, sps_done
= 0;
71 const uint8_t *extradata
= avctx
->extradata
+4;
72 static const uint8_t nalu_header
[4] = {0, 0, 0, 1};
74 /* retrieve length coded size */
75 ctx
->length_size
= (*extradata
++ & 0x3) + 1;
76 if (ctx
->length_size
== 3)
77 return AVERROR(EINVAL
);
79 /* retrieve sps and pps unit(s) */
80 unit_nb
= *extradata
++ & 0x1f; /* number of sps unit(s) */
82 unit_nb
= *extradata
++; /* number of pps unit(s) */
86 unit_size
= AV_RB16(extradata
);
87 total_size
+= unit_size
+4;
88 if (extradata
+2+unit_size
> avctx
->extradata
+avctx
->extradata_size
) {
90 return AVERROR(EINVAL
);
92 out
= av_realloc(out
, total_size
);
94 return AVERROR(ENOMEM
);
95 memcpy(out
+total_size
-unit_size
-4, nalu_header
, 4);
96 memcpy(out
+total_size
-unit_size
, extradata
+2, unit_size
);
97 extradata
+= 2+unit_size
;
99 if (!unit_nb
&& !sps_done
++)
100 unit_nb
= *extradata
++; /* number of pps unit(s) */
103 ctx
->sps_pps_data
= out
;
104 ctx
->size
= total_size
;
111 if (ctx
->length_size
== 1)
113 else if (ctx
->length_size
== 2)
114 nal_size
= AV_RB16(buf
);
116 nal_size
= AV_RB32(buf
);
118 buf
+= ctx
->length_size
;
119 unit_type
= *buf
& 0x1f;
121 /* prepend only to the first type 5 NAL unit of an IDR picture */
122 if (ctx
->first_idr
&& unit_type
== 5) {
123 alloc_and_copy(poutbuf
, poutbuf_size
,
124 ctx
->sps_pps_data
, ctx
->size
,
129 alloc_and_copy(poutbuf
, poutbuf_size
,
132 if (!ctx
->first_idr
&& unit_type
== 1)
137 cumul_size
+= nal_size
+ ctx
->length_size
;
138 } while (cumul_size
< buf_size
);
143 static void h264_mp4toannexb_close(AVBitStreamFilterContext
*bsfc
)
145 H264BSFContext
*ctx
= bsfc
->priv_data
;
146 av_freep(&ctx
->sps_pps_data
);
149 AVBitStreamFilter h264_mp4toannexb_bsf
= {
151 sizeof(H264BSFContext
),
152 h264_mp4toannexb_filter
,
153 h264_mp4toannexb_close
,