Skip to content

Instantly share code, notes, and snippets.

@moonpfe
Created August 24, 2018 05:06
Show Gist options
  • Save moonpfe/f6795d51294d91ee0f82f62ff6985db0 to your computer and use it in GitHub Desktop.
Save moonpfe/f6795d51294d91ee0f82f62ff6985db0 to your computer and use it in GitHub Desktop.
Get codec extradata from video frame
static uint8_t *
mp4serv_get_codec_extradata (EdcBuf *frame,
int *extradata_size)
{
AVBSFContext *bsf;
const AVBitStreamFilter *f;
const enum AVCodecID *ids;
AVPacket *dst_pkt;
int avcodec_id;
int found;
int ret;
AVPacket pkt;
AVPacket *pkt_ref;
uint8_t *data;
data = NULL;
if (extradata_size)
*extradata_size = 0;
f = av_bsf_get_by_name ("extract_extradata");
if (!f || !f->codec_ids)
return data;
avcodec_id = edc_codec_lookup_avcodec_id (frame->codec_id);
found = 0;
for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
{
if (*ids == avcodec_id)
{
found = 1;
break;
}
}
if (!found)
return data;
bsf = NULL;
ret = av_bsf_alloc (f, &bsf);
if (ret < 0)
return data;
bsf->par_in->codec_id = avcodec_id;
ret = av_bsf_init (bsf);
if (ret < 0)
{
av_bsf_free (&bsf);
return data;
}
dst_pkt = av_packet_alloc ();
pkt_ref = dst_pkt;
av_init_packet (&pkt);
pkt.data = (uint8_t *) frame->data;
pkt.size = frame->len;
if (frame->flags & EDC_BUF_FLAG_KEY_FRAME)
pkt.flags |= AV_PKT_FLAG_KEY;
ret = av_packet_ref (pkt_ref, &pkt);
if (ret < 0)
goto end_of_extract_extradata;
ret = av_bsf_send_packet (bsf, pkt_ref);
if (ret < 0)
{
av_packet_unref (pkt_ref);
goto end_of_extract_extradata;
}
ret = 0;
while (ret >= 0)
{
uint8_t *extra;
int extra_size;
ret = av_bsf_receive_packet (bsf, pkt_ref);
if (ret < 0)
{
if (ret != AVERROR (EAGAIN) && ret != AVERROR_EOF)
break;
continue;
}
extra = av_packet_get_side_data (pkt_ref,
AV_PKT_DATA_NEW_EXTRADATA,
&extra_size);
if (extra && extra_size > 0)
{
data = g_malloc0 (extra_size + AV_INPUT_BUFFER_PADDING_SIZE);
memcpy (data, extra, extra_size);
if (extradata_size)
*extradata_size = extra_size;
av_packet_unref (pkt_ref);
break;
}
av_packet_unref (pkt_ref);
}
end_of_extract_extradata:
av_bsf_free (&bsf);
av_packet_free (&dst_pkt);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment