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, 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
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 goto pps;
00091 } else {
00092 sps_seen = 1;
00093 }
00094
00095 while (unit_nb--) {
00096 void *tmp;
00097
00098 unit_size = AV_RB16(extradata);
00099 total_size += unit_size+4;
00100 if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE ||
00101 extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
00102 av_free(out);
00103 return AVERROR(EINVAL);
00104 }
00105 tmp = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE);
00106 if (!tmp) {
00107 av_free(out);
00108 return AVERROR(ENOMEM);
00109 }
00110 out = tmp;
00111 memcpy(out+total_size-unit_size-4, nalu_header, 4);
00112 memcpy(out+total_size-unit_size, extradata+2, unit_size);
00113 extradata += 2+unit_size;
00114 pps:
00115 if (!unit_nb && !sps_done++) {
00116 unit_nb = *extradata++;
00117 if (unit_nb)
00118 pps_seen = 1;
00119 }
00120 }
00121
00122 if(out)
00123 memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00124
00125 if (!sps_seen)
00126 av_log(avctx, AV_LOG_WARNING, "Warning: SPS NALU missing or invalid. The resulting stream may not play.\n");
00127 if (!pps_seen)
00128 av_log(avctx, AV_LOG_WARNING, "Warning: PPS NALU missing or invalid. The resulting stream may not play.\n");
00129
00130 av_free(avctx->extradata);
00131 avctx->extradata = out;
00132 avctx->extradata_size = total_size;
00133 ctx->first_idr = 1;
00134 ctx->extradata_parsed = 1;
00135 }
00136
00137 *poutbuf_size = 0;
00138 *poutbuf = NULL;
00139 do {
00140 if (buf + ctx->length_size > buf_end)
00141 goto fail;
00142
00143 if (ctx->length_size == 1) {
00144 nal_size = buf[0];
00145 } else if (ctx->length_size == 2) {
00146 nal_size = AV_RB16(buf);
00147 } else
00148 nal_size = AV_RB32(buf);
00149
00150 buf += ctx->length_size;
00151 unit_type = *buf & 0x1f;
00152
00153 if (buf + nal_size > buf_end || nal_size < 0)
00154 goto fail;
00155
00156
00157 if (ctx->first_idr && unit_type == 5) {
00158 if (alloc_and_copy(poutbuf, poutbuf_size,
00159 avctx->extradata, avctx->extradata_size,
00160 buf, nal_size) < 0)
00161 goto fail;
00162 ctx->first_idr = 0;
00163 } else {
00164 if (alloc_and_copy(poutbuf, poutbuf_size,
00165 NULL, 0,
00166 buf, nal_size) < 0)
00167 goto fail;
00168 if (!ctx->first_idr && unit_type == 1)
00169 ctx->first_idr = 1;
00170 }
00171
00172 buf += nal_size;
00173 cumul_size += nal_size + ctx->length_size;
00174 } while (cumul_size < buf_size);
00175
00176 return 1;
00177
00178 fail:
00179 av_freep(poutbuf);
00180 *poutbuf_size = 0;
00181 return AVERROR(EINVAL);
00182 }
00183
00184 AVBitStreamFilter ff_h264_mp4toannexb_bsf = {
00185 "h264_mp4toannexb",
00186 sizeof(H264BSFContext),
00187 h264_mp4toannexb_filter,
00188 };
00189