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