00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00068 if (!avctx->extradata || avctx->extradata_size < 6) {
00069 *poutbuf = (uint8_t*) buf;
00070 *poutbuf_size = buf_size;
00071 return 0;
00072 }
00073
00074
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;
00079 const uint8_t *extradata = avctx->extradata+4;
00080 static const uint8_t nalu_header[4] = {0, 0, 0, 1};
00081
00082
00083 ctx->length_size = (*extradata++ & 0x3) + 1;
00084 if (ctx->length_size == 3)
00085 return AVERROR(EINVAL);
00086
00087
00088 unit_nb = *extradata++ & 0x1f;
00089 if (!unit_nb) {
00090 unit_nb = *extradata++;
00091 sps_done++;
00092 }
00093 while (unit_nb--) {
00094 void *tmp;
00095
00096 unit_size = AV_RB16(extradata);
00097 total_size += unit_size+4;
00098 if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE ||
00099 extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
00100 av_free(out);
00101 return AVERROR(EINVAL);
00102 }
00103 tmp = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE);
00104 if (!tmp) {
00105 av_free(out);
00106 return AVERROR(ENOMEM);
00107 }
00108 out = tmp;
00109 memcpy(out+total_size-unit_size-4, nalu_header, 4);
00110 memcpy(out+total_size-unit_size, extradata+2, unit_size);
00111 extradata += 2+unit_size;
00112
00113 if (!unit_nb && !sps_done++)
00114 unit_nb = *extradata++;
00115 }
00116
00117 memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00118 av_free(avctx->extradata);
00119 avctx->extradata = out;
00120 avctx->extradata_size = total_size;
00121 ctx->first_idr = 1;
00122 ctx->extradata_parsed = 1;
00123 }
00124
00125 *poutbuf_size = 0;
00126 *poutbuf = NULL;
00127 do {
00128 if (buf + ctx->length_size > buf_end)
00129 goto fail;
00130
00131 if (ctx->length_size == 1) {
00132 nal_size = buf[0];
00133 } else if (ctx->length_size == 2) {
00134 nal_size = AV_RB16(buf);
00135 } else
00136 nal_size = AV_RB32(buf);
00137
00138 buf += ctx->length_size;
00139 unit_type = *buf & 0x1f;
00140
00141 if (buf + nal_size > buf_end || nal_size < 0)
00142 goto fail;
00143
00144
00145 if (ctx->first_idr && unit_type == 5) {
00146 if (alloc_and_copy(poutbuf, poutbuf_size,
00147 avctx->extradata, avctx->extradata_size,
00148 buf, nal_size) < 0)
00149 goto fail;
00150 ctx->first_idr = 0;
00151 } else {
00152 if (alloc_and_copy(poutbuf, poutbuf_size,
00153 NULL, 0,
00154 buf, nal_size) < 0)
00155 goto fail;
00156 if (!ctx->first_idr && unit_type == 1)
00157 ctx->first_idr = 1;
00158 }
00159
00160 buf += nal_size;
00161 cumul_size += nal_size + ctx->length_size;
00162 } while (cumul_size < buf_size);
00163
00164 return 1;
00165
00166 fail:
00167 av_freep(poutbuf);
00168 *poutbuf_size = 0;
00169 return AVERROR(EINVAL);
00170 }
00171
00172 AVBitStreamFilter h264_mp4toannexb_bsf = {
00173 "h264_mp4toannexb",
00174 sizeof(H264BSFContext),
00175 h264_mp4toannexb_filter,
00176 };
00177