mirror of
https://github.com/tuxbox-neutrino/libstb-hal.git
synced 2025-08-28 07:51:21 +02:00
Refactoring debug code
Conflicts: libeplayer3-arm/container/container_ffmpeg.c
This commit is contained in:
269
libeplayer3-arm/container/mpeg4p2_ffmpeg.c
Normal file → Executable file
269
libeplayer3-arm/container/mpeg4p2_ffmpeg.c
Normal file → Executable file
@@ -3,108 +3,46 @@
|
||||
// http://forums.openpli.org/topic/39326-gstreamer10-and-mpeg4-part2/?hl=%2Bmpeg4+%2Bpart2
|
||||
//
|
||||
|
||||
#define MPEG4P2_MAX_B_FRAMES_COUNT 5
|
||||
|
||||
// mpeg4_unpack_bframes
|
||||
typedef struct
|
||||
{
|
||||
int b_frames_count;
|
||||
int first_ip_frame_written;
|
||||
int64_t packet_duration;
|
||||
AVPacket *b_frames[MPEG4P2_MAX_B_FRAMES_COUNT];
|
||||
AVPacket *second_ip_frame;
|
||||
const AVBitStreamFilter *bsf;
|
||||
AVBSFContext *ctx;
|
||||
} Mpeg4P2Context;
|
||||
|
||||
|
||||
static void set_packet(AVPacket **pkt_dest, AVPacket *pkt_src)
|
||||
static Mpeg4P2Context *mpeg4p2_context_open()
|
||||
{
|
||||
if (pkt_dest == NULL)
|
||||
return;
|
||||
if (*pkt_dest != NULL)
|
||||
Mpeg4P2Context *context = NULL;
|
||||
const AVBitStreamFilter *bsf = av_bsf_get_by_name("mpeg4_unpack_bframes");
|
||||
if (bsf)
|
||||
{
|
||||
wrapped_packet_unref(*pkt_dest);
|
||||
av_free(*pkt_dest);
|
||||
}
|
||||
*pkt_dest = av_malloc(sizeof(AVPacket));
|
||||
av_packet_ref(*pkt_dest, pkt_src);
|
||||
}
|
||||
|
||||
static int filter_packet(AVBitStreamFilterContext *bsf_ctx, AVCodecContext *enc_ctx, AVPacket *pkt)
|
||||
{
|
||||
int ret;
|
||||
AVPacket new_pkt = *pkt;
|
||||
ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL,
|
||||
&new_pkt.data, &new_pkt.size,
|
||||
pkt->data, pkt->size,
|
||||
pkt->flags & AV_PKT_FLAG_KEY);
|
||||
if (ret == 0 && new_pkt.data != pkt->data)
|
||||
{
|
||||
if ((ret = av_packet_ref(&new_pkt, pkt)) < 0)
|
||||
return -1;
|
||||
ret = 1;
|
||||
}
|
||||
if (ret > 0)
|
||||
{
|
||||
pkt->side_data = NULL;
|
||||
pkt->side_data_elems = 0;
|
||||
wrapped_packet_unref(pkt);
|
||||
new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
|
||||
av_buffer_default_free, NULL, 0);
|
||||
if (!new_pkt.buf)
|
||||
return -1;
|
||||
}
|
||||
if (ret < 0)
|
||||
{
|
||||
ffmpeg_err("Failed to filter bitstream with filter %s for stream %d with codec %s\n",
|
||||
bsf_ctx->filter->name, pkt->stream_index,
|
||||
avcodec_get_name(enc_ctx->codec_id));
|
||||
return -1;
|
||||
}
|
||||
*pkt = new_pkt;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mpeg4p2_context_reset(Mpeg4P2Context *context)
|
||||
{
|
||||
if (context == NULL)
|
||||
return;
|
||||
int i;
|
||||
for (i = 0; i < MPEG4P2_MAX_B_FRAMES_COUNT; i++)
|
||||
{
|
||||
if (context->b_frames[i] != NULL)
|
||||
context = malloc(sizeof(Mpeg4P2Context));
|
||||
if (context)
|
||||
{
|
||||
wrapped_packet_unref(context->b_frames[i]);
|
||||
av_free(context->b_frames[i]);
|
||||
memset(context, 0x00, sizeof(Mpeg4P2Context));
|
||||
context->bsf = bsf;
|
||||
}
|
||||
context->b_frames[i] = NULL;
|
||||
}
|
||||
if (context->second_ip_frame != NULL)
|
||||
{
|
||||
wrapped_packet_unref(context->second_ip_frame);
|
||||
av_free(context->second_ip_frame);
|
||||
}
|
||||
context->second_ip_frame = NULL;
|
||||
|
||||
context->b_frames_count = 0;
|
||||
context->first_ip_frame_written = 0;
|
||||
context->packet_duration = 0;
|
||||
return context;
|
||||
}
|
||||
|
||||
static void mpeg4p2_write(Context_t *ctx, Track_t *track, int avContextIdx, int64_t *pts_current, int64_t *pts_latest, AVPacket *pkt)
|
||||
static void mpeg4p2_write(Context_t *ctx, Mpeg4P2Context *mpeg4p2_ctx, Track_t *track, int64_t start_time, int64_t *currentVideoPts, int64_t *latestPts, AVPacket *pkt)
|
||||
{
|
||||
*pts_current = track->pts = calcPts(avContextIdx, track->stream, pkt->pts);
|
||||
if ((*pts_current > *pts_latest) && (*pts_current != INVALID_PTS_VALUE))
|
||||
*currentVideoPts = track->pts = doCalcPts(start_time, mpeg4p2_ctx->ctx->time_base_out, pkt->pts);
|
||||
if ((*currentVideoPts > *latestPts) && (*currentVideoPts != INVALID_PTS_VALUE))
|
||||
{
|
||||
*pts_latest = *pts_current;
|
||||
*latestPts = *currentVideoPts;
|
||||
}
|
||||
track->dts = calcPts(avContextIdx, track->stream, pkt->dts);
|
||||
|
||||
track->dts = doCalcPts(start_time, mpeg4p2_ctx->ctx->time_base_out, pkt->dts);
|
||||
|
||||
AudioVideoOut_t avOut;
|
||||
avOut.data = pkt->data;
|
||||
avOut.len = pkt->size;
|
||||
avOut.pts = track->pts;
|
||||
avOut.dts = track->dts;
|
||||
avOut.extradata = track->extraData;
|
||||
avOut.extralen = track->extraSize;
|
||||
avOut.extradata = mpeg4p2_ctx->ctx->par_out->extradata;
|
||||
avOut.extralen = mpeg4p2_ctx->ctx->par_out->extradata_size;
|
||||
avOut.frameRate = track->frame_rate;
|
||||
avOut.timeScale = track->TimeScale;
|
||||
avOut.width = track->width;
|
||||
@@ -117,97 +55,90 @@ static void mpeg4p2_write(Context_t *ctx, Track_t *track, int avContextIdx, int6
|
||||
}
|
||||
}
|
||||
|
||||
static int mpeg4p2_write_packet(Context_t *ctx, Mpeg4P2Context *mpeg4p2_ctx, Track_t *track, int cAVIdx, int64_t *pts_current, int64_t *pts_latest, AVPacket *pkt)
|
||||
static int mpeg4p2_context_reset(Mpeg4P2Context *context)
|
||||
{
|
||||
uint8_t *data = pkt->data;
|
||||
int data_len = pkt->size;
|
||||
int pos = 0;
|
||||
if (mpeg4p2_ctx->packet_duration == 0)
|
||||
int ret = 0;
|
||||
if (context && context->ctx)
|
||||
{
|
||||
mpeg4p2_ctx->packet_duration = pkt->duration;
|
||||
}
|
||||
while (pos < data_len)
|
||||
{
|
||||
if (memcmp(&data[pos], "\x00\x00\x01\xb6", 4))
|
||||
// Flush
|
||||
ret = av_bsf_send_packet(context->ctx, NULL);
|
||||
if (ret == 0)
|
||||
{
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
pos += 4;
|
||||
switch ((data[pos] & 0xC0) >> 6)
|
||||
{
|
||||
case 0: // I-Frame
|
||||
case 1: // P-Frame
|
||||
if (!mpeg4p2_ctx->first_ip_frame_written)
|
||||
{
|
||||
mpeg4p2_ctx->first_ip_frame_written = 1;
|
||||
pkt->pts = pkt->dts + mpeg4p2_ctx->packet_duration;
|
||||
ffmpeg_printf(100, "Writing first I/P packet\n");
|
||||
mpeg4p2_write(ctx, track, cAVIdx, pts_current, pts_latest, pkt);
|
||||
return 0;
|
||||
}
|
||||
else if (!mpeg4p2_ctx->second_ip_frame)
|
||||
{
|
||||
set_packet(&mpeg4p2_ctx->second_ip_frame, pkt);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!mpeg4p2_ctx->b_frames_count)
|
||||
{
|
||||
mpeg4p2_ctx->second_ip_frame->pts = mpeg4p2_ctx->second_ip_frame->dts + mpeg4p2_ctx->packet_duration;
|
||||
ffmpeg_printf(100, "Writing second I/P packet(1)\n");
|
||||
mpeg4p2_write(ctx, track, cAVIdx, pts_current, pts_latest, mpeg4p2_ctx->second_ip_frame);
|
||||
set_packet(&mpeg4p2_ctx->second_ip_frame, pkt);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
mpeg4p2_ctx->second_ip_frame->pts = mpeg4p2_ctx->b_frames[mpeg4p2_ctx->b_frames_count - 1]->dts + mpeg4p2_ctx->packet_duration;
|
||||
mpeg4p2_ctx->b_frames[0]->pts = mpeg4p2_ctx->second_ip_frame->dts + mpeg4p2_ctx->packet_duration;
|
||||
int i;
|
||||
for (i = 1; i < mpeg4p2_ctx->b_frames_count; i++)
|
||||
{
|
||||
mpeg4p2_ctx->b_frames[i]->pts = mpeg4p2_ctx->b_frames[i - 1]->dts + mpeg4p2_ctx->packet_duration;
|
||||
}
|
||||
ffmpeg_printf(100, "Writing second I/P packet(2)\n");
|
||||
mpeg4p2_write(ctx, track, cAVIdx, pts_current, pts_latest, mpeg4p2_ctx->second_ip_frame);
|
||||
set_packet(&mpeg4p2_ctx->second_ip_frame, pkt);
|
||||
for (i = 0; i < mpeg4p2_ctx->b_frames_count; i++)
|
||||
{
|
||||
ffmpeg_printf(100, "Writing B-frame[%d]\n", i);
|
||||
mpeg4p2_write(ctx, track, cAVIdx, pts_current, pts_latest, mpeg4p2_ctx->b_frames[i]);
|
||||
}
|
||||
mpeg4p2_ctx->b_frames_count = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3: // S-Frame
|
||||
break;
|
||||
case 2: // B-Frame
|
||||
if (!mpeg4p2_ctx->second_ip_frame)
|
||||
{
|
||||
ffmpeg_err("Cannot predict B-Frame without surrounding I/P-Frames, dropping...");
|
||||
return 0;
|
||||
}
|
||||
if (mpeg4p2_ctx->b_frames_count == MPEG4P2_MAX_B_FRAMES_COUNT)
|
||||
{
|
||||
ffmpeg_err("Oops max B-Frames count = %d, reached", MPEG4P2_MAX_B_FRAMES_COUNT);
|
||||
// not recoverable, to fix just increase MPEG4P2_MAX_B_FRAMES_COUNT
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ffmpeg_printf(100, "Storing B-Frame\n");
|
||||
set_packet(&mpeg4p2_ctx->b_frames[mpeg4p2_ctx->b_frames_count++], pkt);
|
||||
return 0;
|
||||
}
|
||||
case 4:
|
||||
default:
|
||||
break;
|
||||
AVPacket *pkt = NULL;
|
||||
while ((ret = av_bsf_receive_packet(context->ctx, pkt)) == 0)
|
||||
{
|
||||
wrapped_frame_unref(pkt);
|
||||
}
|
||||
}
|
||||
av_bsf_free(&context->ctx);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int mpeg4p2_write_packet(Context_t *ctx, Mpeg4P2Context *mpeg4p2_ctx, Track_t *track, int cAVIdx, int64_t *pts_current, int64_t *pts_latest, AVPacket *pkt)
|
||||
{
|
||||
int ret = 0;
|
||||
if (mpeg4p2_ctx)
|
||||
{
|
||||
// Setup is needed
|
||||
if (!mpeg4p2_ctx->ctx)
|
||||
{
|
||||
ret = av_bsf_alloc(mpeg4p2_ctx->bsf, &mpeg4p2_ctx->ctx);
|
||||
if (ret == 0)
|
||||
{
|
||||
AVStream *in = track->stream;
|
||||
ret = avcodec_parameters_copy(mpeg4p2_ctx->ctx->par_in, in->codecpar);
|
||||
if (ret == 0)
|
||||
{
|
||||
mpeg4p2_ctx->ctx->time_base_in = in->time_base;
|
||||
ret = av_bsf_init(mpeg4p2_ctx->ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == 0)
|
||||
{
|
||||
ret = av_bsf_send_packet(mpeg4p2_ctx->ctx, pkt);
|
||||
if (ret == 0)
|
||||
{
|
||||
while ((ret = av_bsf_receive_packet(mpeg4p2_ctx->ctx, pkt)) == 0)
|
||||
{
|
||||
mpeg4p2_write(ctx, mpeg4p2_ctx, track, avContextTab[cAVIdx]->start_time, pts_current, pts_latest, pkt);
|
||||
}
|
||||
|
||||
if (ret == AVERROR(EAGAIN))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
ffmpeg_err("av_bsf_receive_packet failed error 0x%x\n", ret);
|
||||
mpeg4p2_context_reset(mpeg4p2_ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ffmpeg_err("bsf setup failed error 0x%x\n", ret);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void mpeg4p2_context_close(Mpeg4P2Context *context)
|
||||
{
|
||||
if (context)
|
||||
{
|
||||
mpeg4p2_context_reset(context);
|
||||
free(context);
|
||||
return;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user