00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022
00023 typedef struct H264BSFContext {
00024 uint8_t length_size;
00025 uint8_t first_idr;
00026 uint8_t *sps_pps_data;
00027 uint32_t size;
00028 } H264BSFContext;
00029
00030 static void alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
00031 const uint8_t *sps_pps, uint32_t sps_pps_size,
00032 const uint8_t *in, uint32_t in_size) {
00033 uint32_t offset = *poutbuf_size;
00034 uint8_t nal_header_size = offset ? 3 : 4;
00035
00036 *poutbuf_size += sps_pps_size+in_size+nal_header_size;
00037 *poutbuf = av_realloc(*poutbuf, *poutbuf_size);
00038 if (sps_pps)
00039 memcpy(*poutbuf+offset, sps_pps, sps_pps_size);
00040 memcpy(*poutbuf+sps_pps_size+nal_header_size+offset, in, in_size);
00041 if (!offset)
00042 AV_WB32(*poutbuf+sps_pps_size, 1);
00043 else {
00044 (*poutbuf+offset+sps_pps_size)[0] = (*poutbuf+offset+sps_pps_size)[1] = 0;
00045 (*poutbuf+offset+sps_pps_size)[2] = 1;
00046 }
00047 }
00048
00049 static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
00050 AVCodecContext *avctx, const char *args,
00051 uint8_t **poutbuf, int *poutbuf_size,
00052 const uint8_t *buf, int buf_size,
00053 int keyframe) {
00054 H264BSFContext *ctx = bsfc->priv_data;
00055 uint8_t unit_type;
00056 uint32_t nal_size, cumul_size = 0;
00057
00058
00059 if (!avctx->extradata || avctx->extradata_size < 6) {
00060 *poutbuf = (uint8_t*) buf;
00061 *poutbuf_size = buf_size;
00062 return 0;
00063 }
00064
00065
00066 if (!ctx->sps_pps_data) {
00067 uint16_t unit_size;
00068 uint32_t total_size = 0;
00069 uint8_t *out = NULL, unit_nb, sps_done = 0;
00070 const uint8_t *extradata = avctx->extradata+4;
00071 static const uint8_t nalu_header[4] = {0, 0, 0, 1};
00072
00073
00074 ctx->length_size = (*extradata++ & 0x3) + 1;
00075 if (ctx->length_size == 3)
00076 return AVERROR(EINVAL);
00077
00078
00079 unit_nb = *extradata++ & 0x1f;
00080 if (!unit_nb) {
00081 unit_nb = *extradata++;
00082 sps_done++;
00083 }
00084 while (unit_nb--) {
00085 unit_size = AV_RB16(extradata);
00086 total_size += unit_size+4;
00087 if (extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
00088 av_free(out);
00089 return AVERROR(EINVAL);
00090 }
00091 out = av_realloc(out, total_size);
00092 if (!out)
00093 return AVERROR(ENOMEM);
00094 memcpy(out+total_size-unit_size-4, nalu_header, 4);
00095 memcpy(out+total_size-unit_size, extradata+2, unit_size);
00096 extradata += 2+unit_size;
00097
00098 if (!unit_nb && !sps_done++)
00099 unit_nb = *extradata++;
00100 }
00101
00102 ctx->sps_pps_data = out;
00103 ctx->size = total_size;
00104 ctx->first_idr = 1;
00105 }
00106
00107 *poutbuf_size = 0;
00108 *poutbuf = NULL;
00109 do {
00110 if (ctx->length_size == 1)
00111 nal_size = buf[0];
00112 else if (ctx->length_size == 2)
00113 nal_size = AV_RB16(buf);
00114 else
00115 nal_size = AV_RB32(buf);
00116
00117 buf += ctx->length_size;
00118 unit_type = *buf & 0x1f;
00119
00120
00121 if (ctx->first_idr && unit_type == 5) {
00122 alloc_and_copy(poutbuf, poutbuf_size,
00123 ctx->sps_pps_data, ctx->size,
00124 buf, nal_size);
00125 ctx->first_idr = 0;
00126 }
00127 else {
00128 alloc_and_copy(poutbuf, poutbuf_size,
00129 NULL, 0,
00130 buf, nal_size);
00131 if (!ctx->first_idr && unit_type == 1)
00132 ctx->first_idr = 1;
00133 }
00134
00135 buf += nal_size;
00136 cumul_size += nal_size + ctx->length_size;
00137 } while (cumul_size < buf_size);
00138
00139 return 1;
00140 }
00141
00142 static void h264_mp4toannexb_close(AVBitStreamFilterContext *bsfc)
00143 {
00144 H264BSFContext *ctx = bsfc->priv_data;
00145 av_freep(&ctx->sps_pps_data);
00146 }
00147
00148 AVBitStreamFilter h264_mp4toannexb_bsf = {
00149 "h264_mp4toannexb",
00150 sizeof(H264BSFContext),
00151 h264_mp4toannexb_filter,
00152 h264_mp4toannexb_close,
00153 };
00154