Libav 0.7.1
|
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 Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; 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 00067 /* nothing to filter */ 00068 if (!avctx->extradata || avctx->extradata_size < 6) { 00069 *poutbuf = (uint8_t*) buf; 00070 *poutbuf_size = buf_size; 00071 return 0; 00072 } 00073 00074 /* retrieve sps and pps NAL units from extradata */ 00075 if (!ctx->extradata_parsed) { 00076 uint16_t unit_size; 00077 uint64_t total_size = 0; 00078 uint8_t *out = NULL, unit_nb, sps_done = 0, sps_seen = 0, pps_seen = 0; 00079 const uint8_t *extradata = avctx->extradata+4; 00080 static const uint8_t nalu_header[4] = {0, 0, 0, 1}; 00081 00082 /* retrieve length coded size */ 00083 ctx->length_size = (*extradata++ & 0x3) + 1; 00084 if (ctx->length_size == 3) 00085 return AVERROR(EINVAL); 00086 00087 /* retrieve sps and pps unit(s) */ 00088 unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */ 00089 if (!unit_nb) { 00090 unit_nb = *extradata++; /* number of pps unit(s) */ 00091 sps_done++; 00092 00093 if (unit_nb) 00094 pps_seen = 1; 00095 } else { 00096 sps_seen = 1; 00097 } 00098 00099 while (unit_nb--) { 00100 void *tmp; 00101 00102 unit_size = AV_RB16(extradata); 00103 total_size += unit_size+4; 00104 if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE || 00105 extradata+2+unit_size > avctx->extradata+avctx->extradata_size) { 00106 av_free(out); 00107 return AVERROR(EINVAL); 00108 } 00109 tmp = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE); 00110 if (!tmp) { 00111 av_free(out); 00112 return AVERROR(ENOMEM); 00113 } 00114 out = tmp; 00115 memcpy(out+total_size-unit_size-4, nalu_header, 4); 00116 memcpy(out+total_size-unit_size, extradata+2, unit_size); 00117 extradata += 2+unit_size; 00118 00119 if (!unit_nb && !sps_done++) { 00120 unit_nb = *extradata++; /* number of pps unit(s) */ 00121 if (unit_nb) 00122 pps_seen = 1; 00123 } 00124 } 00125 00126 if(out) 00127 memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 00128 00129 if (!sps_seen) 00130 av_log(avctx, AV_LOG_WARNING, "Warning: SPS NALU missing or invalid. The resulting stream may not play.\n"); 00131 if (!pps_seen) 00132 av_log(avctx, AV_LOG_WARNING, "Warning: PPS NALU missing or invalid. The resulting stream may not play.\n"); 00133 00134 av_free(avctx->extradata); 00135 avctx->extradata = out; 00136 avctx->extradata_size = total_size; 00137 ctx->first_idr = 1; 00138 ctx->extradata_parsed = 1; 00139 } 00140 00141 *poutbuf_size = 0; 00142 *poutbuf = NULL; 00143 do { 00144 if (buf + ctx->length_size > buf_end) 00145 goto fail; 00146 00147 if (ctx->length_size == 1) { 00148 nal_size = buf[0]; 00149 } else if (ctx->length_size == 2) { 00150 nal_size = AV_RB16(buf); 00151 } else 00152 nal_size = AV_RB32(buf); 00153 00154 buf += ctx->length_size; 00155 unit_type = *buf & 0x1f; 00156 00157 if (buf + nal_size > buf_end || nal_size < 0) 00158 goto fail; 00159 00160 /* prepend only to the first type 5 NAL unit of an IDR picture */ 00161 if (ctx->first_idr && unit_type == 5) { 00162 if (alloc_and_copy(poutbuf, poutbuf_size, 00163 avctx->extradata, avctx->extradata_size, 00164 buf, nal_size) < 0) 00165 goto fail; 00166 ctx->first_idr = 0; 00167 } else { 00168 if (alloc_and_copy(poutbuf, poutbuf_size, 00169 NULL, 0, 00170 buf, nal_size) < 0) 00171 goto fail; 00172 if (!ctx->first_idr && unit_type == 1) 00173 ctx->first_idr = 1; 00174 } 00175 00176 buf += nal_size; 00177 cumul_size += nal_size + ctx->length_size; 00178 } while (cumul_size < buf_size); 00179 00180 return 1; 00181 00182 fail: 00183 av_freep(poutbuf); 00184 *poutbuf_size = 0; 00185 return AVERROR(EINVAL); 00186 } 00187 00188 AVBitStreamFilter ff_h264_mp4toannexb_bsf = { 00189 "h264_mp4toannexb", 00190 sizeof(H264BSFContext), 00191 h264_mp4toannexb_filter, 00192 }; 00193