• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/h264_mp4toannexb_bsf.c

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

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1