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 int i;
00063 uint8_t unit_type;
00064 int32_t nal_size;
00065 uint32_t cumul_size = 0;
00066 const uint8_t *buf_end = buf + buf_size;
00067 int ret = AVERROR(EINVAL);
00068
00069
00070 if (!avctx->extradata || avctx->extradata_size < 6) {
00071 *poutbuf = (uint8_t*) buf;
00072 *poutbuf_size = buf_size;
00073 return 0;
00074 }
00075
00076
00077 if (!ctx->extradata_parsed) {
00078 uint16_t unit_size;
00079 uint64_t total_size = 0;
00080 uint8_t *out = NULL, unit_nb, sps_done = 0, sps_seen = 0, pps_seen = 0;
00081 const uint8_t *extradata = avctx->extradata+4;
00082 static const uint8_t nalu_header[4] = {0, 0, 0, 1};
00083
00084
00085 ctx->length_size = (*extradata++ & 0x3) + 1;
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 ret= AVERROR(EINVAL);
00141 if (buf + ctx->length_size > buf_end)
00142 goto fail;
00143
00144 for (nal_size = 0, i = 0; i<ctx->length_size; i++)
00145 nal_size = (nal_size << 8) | buf[i];
00146
00147 buf += ctx->length_size;
00148 unit_type = *buf & 0x1f;
00149
00150 if (buf + nal_size > buf_end || nal_size < 0)
00151 goto fail;
00152
00153
00154 if (ctx->first_idr && unit_type == 5) {
00155 if ((ret=alloc_and_copy(poutbuf, poutbuf_size,
00156 avctx->extradata, avctx->extradata_size,
00157 buf, nal_size)) < 0)
00158 goto fail;
00159 ctx->first_idr = 0;
00160 } else {
00161 if ((ret=alloc_and_copy(poutbuf, poutbuf_size,
00162 NULL, 0,
00163 buf, nal_size)) < 0)
00164 goto fail;
00165 if (!ctx->first_idr && unit_type == 1)
00166 ctx->first_idr = 1;
00167 }
00168
00169 buf += nal_size;
00170 cumul_size += nal_size + ctx->length_size;
00171 } while (cumul_size < buf_size);
00172
00173 return 1;
00174
00175 fail:
00176 av_freep(poutbuf);
00177 *poutbuf_size = 0;
00178 return ret;
00179 }
00180
00181 AVBitStreamFilter ff_h264_mp4toannexb_bsf = {
00182 "h264_mp4toannexb",
00183 sizeof(H264BSFContext),
00184 h264_mp4toannexb_filter,
00185 };