libavcodec/h264_mp4toannexb_bsf.c
Go to the documentation of this file.
00001 /*
00002  * H.264 MP4 to Annex B byte stream format filter
00003  * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avcodec.h"
00024 
00025 typedef struct H264BSFContext {
00026     uint8_t  length_size;
00027     uint8_t  first_idr;
00028     int      extradata_parsed;
00029 } H264BSFContext;
00030 
00031 static int alloc_and_copy(uint8_t **poutbuf,          int *poutbuf_size,
00032                           const uint8_t *sps_pps, uint32_t sps_pps_size,
00033                           const uint8_t *in,      uint32_t in_size) {
00034     uint32_t offset = *poutbuf_size;
00035     uint8_t nal_header_size = offset ? 3 : 4;
00036     void *tmp;
00037 
00038     *poutbuf_size += sps_pps_size+in_size+nal_header_size;
00039     tmp = av_realloc(*poutbuf, *poutbuf_size);
00040     if (!tmp)
00041         return AVERROR(ENOMEM);
00042     *poutbuf = tmp;
00043     if (sps_pps)
00044         memcpy(*poutbuf+offset, sps_pps, sps_pps_size);
00045     memcpy(*poutbuf+sps_pps_size+nal_header_size+offset, in, in_size);
00046     if (!offset) {
00047         AV_WB32(*poutbuf+sps_pps_size, 1);
00048     } else {
00049         (*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0;
00050         (*poutbuf+offset+sps_pps_size)[2] = 1;
00051     }
00052 
00053     return 0;
00054 }
00055 
00056 static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
00057                                    AVCodecContext *avctx, const char *args,
00058                                    uint8_t  **poutbuf, int *poutbuf_size,
00059                                    const uint8_t *buf, int      buf_size,
00060                                    int keyframe) {
00061     H264BSFContext *ctx = bsfc->priv_data;
00062     uint8_t unit_type;
00063     int32_t nal_size;
00064     uint32_t cumul_size = 0;
00065     const uint8_t *buf_end = buf + buf_size;
00066     int ret = AVERROR(EINVAL);
00067 
00068     /* nothing to filter */
00069     if (!avctx->extradata || avctx->extradata_size < 6) {
00070         *poutbuf = (uint8_t*) buf;
00071         *poutbuf_size = buf_size;
00072         return 0;
00073     }
00074 
00075     /* retrieve sps and pps NAL units from extradata */
00076     if (!ctx->extradata_parsed) {
00077         uint16_t unit_size;
00078         uint64_t total_size = 0;
00079         uint8_t *out = NULL, unit_nb, sps_done = 0, sps_seen = 0, pps_seen = 0;
00080         const uint8_t *extradata = avctx->extradata+4;
00081         static const uint8_t nalu_header[4] = {0, 0, 0, 1};
00082 
00083         /* retrieve length coded size */
00084         ctx->length_size = (*extradata++ & 0x3) + 1;
00085         if (ctx->length_size == 3)
00086             return AVERROR(EINVAL);
00087 
00088         /* retrieve sps and pps unit(s) */
00089         unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
00090         if (!unit_nb) {
00091             goto pps;
00092         } else {
00093             sps_seen = 1;
00094         }
00095 
00096         while (unit_nb--) {
00097             void *tmp;
00098 
00099             unit_size = AV_RB16(extradata);
00100             total_size += unit_size+4;
00101             if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE ||
00102                 extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
00103                 av_free(out);
00104                 return AVERROR(EINVAL);
00105             }
00106             tmp = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE);
00107             if (!tmp) {
00108                 av_free(out);
00109                 return AVERROR(ENOMEM);
00110             }
00111             out = tmp;
00112             memcpy(out+total_size-unit_size-4, nalu_header, 4);
00113             memcpy(out+total_size-unit_size,   extradata+2, unit_size);
00114             extradata += 2+unit_size;
00115 pps:
00116             if (!unit_nb && !sps_done++) {
00117                 unit_nb = *extradata++; /* number of pps unit(s) */
00118                 if (unit_nb)
00119                     pps_seen = 1;
00120             }
00121         }
00122 
00123         if(out)
00124             memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00125 
00126         if (!sps_seen)
00127             av_log(avctx, AV_LOG_WARNING, "Warning: SPS NALU missing or invalid. The resulting stream may not play.\n");
00128         if (!pps_seen)
00129             av_log(avctx, AV_LOG_WARNING, "Warning: PPS NALU missing or invalid. The resulting stream may not play.\n");
00130 
00131         av_free(avctx->extradata);
00132         avctx->extradata      = out;
00133         avctx->extradata_size = total_size;
00134         ctx->first_idr        = 1;
00135         ctx->extradata_parsed = 1;
00136     }
00137 
00138     *poutbuf_size = 0;
00139     *poutbuf = NULL;
00140     do {
00141         ret= AVERROR(EINVAL);
00142         if (buf + ctx->length_size > buf_end)
00143             goto fail;
00144 
00145         if (ctx->length_size == 1) {
00146             nal_size = buf[0];
00147         } else if (ctx->length_size == 2) {
00148             nal_size = AV_RB16(buf);
00149         } else
00150             nal_size = AV_RB32(buf);
00151 
00152         buf += ctx->length_size;
00153         unit_type = *buf & 0x1f;
00154 
00155         if (buf + nal_size > buf_end || nal_size < 0)
00156             goto fail;
00157 
00158         /* prepend only to the first type 5 NAL unit of an IDR picture */
00159         if (ctx->first_idr && unit_type == 5) {
00160             if ((ret=alloc_and_copy(poutbuf, poutbuf_size,
00161                                avctx->extradata, avctx->extradata_size,
00162                                buf, nal_size)) < 0)
00163                 goto fail;
00164             ctx->first_idr = 0;
00165         } else {
00166             if ((ret=alloc_and_copy(poutbuf, poutbuf_size,
00167                                NULL, 0,
00168                                buf, nal_size)) < 0)
00169                 goto fail;
00170             if (!ctx->first_idr && unit_type == 1)
00171                 ctx->first_idr = 1;
00172         }
00173 
00174         buf += nal_size;
00175         cumul_size += nal_size + ctx->length_size;
00176     } while (cumul_size < buf_size);
00177 
00178     return 1;
00179 
00180 fail:
00181     av_freep(poutbuf);
00182     *poutbuf_size = 0;
00183     return ret;
00184 }
00185 
00186 AVBitStreamFilter ff_h264_mp4toannexb_bsf = {
00187     "h264_mp4toannexb",
00188     sizeof(H264BSFContext),
00189     h264_mp4toannexb_filter,
00190 };
00191