Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264_mp4toannexb_bsf.c @ 6a5d31ac

History | View | Annotate | Download (5.1 KB)

1
/*
2
 * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
3
 *
4
 * This file is part of FFmpeg.
5
 *
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.
10
 *
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.
15
 *
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
19
 */
20

    
21
#include "libavutil/intreadwrite.h"
22
#include "avcodec.h"
23

    
24
typedef struct H264BSFContext {
25
    uint8_t  length_size;
26
    uint8_t  first_idr;
27
    uint8_t *sps_pps_data;
28
    uint32_t size;
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 {
45
        (*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0;
46
        (*poutbuf+offset+sps_pps_size)[2] = 1;
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;
57
    uint32_t nal_size, cumul_size = 0;
58

    
59
    /* nothing to filter */
60
    if (!avctx->extradata || avctx->extradata_size < 6) {
61
        *poutbuf = (uint8_t*) buf;
62
        *poutbuf_size = buf_size;
63
        return 0;
64
    }
65

    
66
    /* retrieve sps and pps NAL units from extradata */
67
    if (!ctx->sps_pps_data) {
68
        uint16_t unit_size;
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};
73

    
74
        /* retrieve length coded size */
75
        ctx->length_size = (*extradata++ & 0x3) + 1;
76
        if (ctx->length_size == 3)
77
            return AVERROR(EINVAL);
78

    
79
        /* retrieve sps and pps unit(s) */
80
        unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
81
        if (!unit_nb) {
82
            unit_nb = *extradata++; /* number of pps unit(s) */
83
            sps_done++;
84
        }
85
        while (unit_nb--) {
86
            unit_size = AV_RB16(extradata);
87
            total_size += unit_size+4;
88
            if (extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
89
                av_free(out);
90
                return AVERROR(EINVAL);
91
            }
92
            out = av_realloc(out, total_size);
93
            if (!out)
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;
98

    
99
            if (!unit_nb && !sps_done++)
100
                unit_nb = *extradata++; /* number of pps unit(s) */
101
        }
102

    
103
        ctx->sps_pps_data = out;
104
        ctx->size = total_size;
105
        ctx->first_idr = 1;
106
    }
107

    
108
    *poutbuf_size = 0;
109
    *poutbuf = NULL;
110
    do {
111
        if (ctx->length_size == 1)
112
            nal_size = buf[0];
113
        else if (ctx->length_size == 2)
114
            nal_size = AV_RB16(buf);
115
        else
116
            nal_size = AV_RB32(buf);
117

    
118
        buf += ctx->length_size;
119
        unit_type = *buf & 0x1f;
120

    
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,
125
                           buf, nal_size);
126
            ctx->first_idr = 0;
127
        }
128
        else {
129
            alloc_and_copy(poutbuf, poutbuf_size,
130
                           NULL, 0,
131
                           buf, nal_size);
132
            if (!ctx->first_idr && unit_type == 1)
133
                ctx->first_idr = 1;
134
        }
135

    
136
        buf += nal_size;
137
        cumul_size += nal_size + ctx->length_size;
138
    } while (cumul_size < buf_size);
139

    
140
    return 1;
141
}
142

    
143
static void h264_mp4toannexb_close(AVBitStreamFilterContext *bsfc)
144
{
145
    H264BSFContext *ctx = bsfc->priv_data;
146
    av_freep(&ctx->sps_pps_data);
147
}
148

    
149
AVBitStreamFilter h264_mp4toannexb_bsf = {
150
    "h264_mp4toannexb",
151
    sizeof(H264BSFContext),
152
    h264_mp4toannexb_filter,
153
    h264_mp4toannexb_close,
154
};
155