Revision d5cc1ed7

View differences:

libavcodec/h264_mp4toannexb_bsf.c
25 25
typedef struct H264BSFContext {
26 26
    uint8_t  length_size;
27 27
    uint8_t  first_idr;
28
    uint8_t *sps_pps_data;
29
    uint32_t size;
28
    int      extradata_parsed;
30 29
} H264BSFContext;
31 30

  
32 31
static void alloc_and_copy(uint8_t **poutbuf,          int *poutbuf_size,
......
67 66
    }
68 67

  
69 68
    /* retrieve sps and pps NAL units from extradata */
70
    if (!ctx->sps_pps_data) {
69
    if (!ctx->extradata_parsed) {
71 70
        uint16_t unit_size;
72
        uint32_t total_size = 0;
71
        uint64_t total_size = 0;
73 72
        uint8_t *out = NULL, unit_nb, sps_done = 0;
74 73
        const uint8_t *extradata = avctx->extradata+4;
75 74
        static const uint8_t nalu_header[4] = {0, 0, 0, 1};
......
88 87
        while (unit_nb--) {
89 88
            unit_size = AV_RB16(extradata);
90 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 91
                av_free(out);
93 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 95
            if (!out)
97 96
                return AVERROR(ENOMEM);
98 97
            memcpy(out+total_size-unit_size-4, nalu_header, 4);
......
103 102
                unit_nb = *extradata++; /* number of pps unit(s) */
104 103
        }
105 104

  
106
        ctx->sps_pps_data = out;
107
        ctx->size = total_size;
108
        ctx->first_idr = 1;
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;
109 111
    }
110 112

  
111 113
    *poutbuf_size = 0;
......
130 132
        /* prepend only to the first type 5 NAL unit of an IDR picture */
131 133
        if (ctx->first_idr && unit_type == 5) {
132 134
            alloc_and_copy(poutbuf, poutbuf_size,
133
                           ctx->sps_pps_data, ctx->size,
135
                           avctx->extradata, avctx->extradata_size,
134 136
                           buf, nal_size);
135 137
            ctx->first_idr = 0;
136 138
        }
......
154 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 159
AVBitStreamFilter h264_mp4toannexb_bsf = {
164 160
    "h264_mp4toannexb",
165 161
    sizeof(H264BSFContext),
166 162
    h264_mp4toannexb_filter,
167
    h264_mp4toannexb_close,
168 163
};
169 164

  

Also available in: Unified diff