Opened 5 years ago

Closed 5 years ago

#2773 closed defect (fixed)

Seeking past the end of an mp3 file has different behavior than other formats

Reported by: wadams Owned by:
Priority: normal Component: avformat
Version: git-master Keywords: mp3 seek regression
Cc: Blocked By:
Blocking: Reproduced by developer: yes
Analyzed by developer: no

Description

I am trying to determine with complete accuracy the length of audio files of various formats by seeking to the near the end and reading packets. This has worked since version 1.2 but since I pulled the latest master branch from git this method no longer works for mp3 files. After I call av_seek_frame() on particular files, the next call to av_read_frame() returns EOF even if I use AVSEEK_FLAG_BACKWARD. Other formats will generally seek to the last sync point in the file when seeking past the end with the backward flag.

I looked through the git history and found the the commit that changed this behavior is e096283ea55bc36a637b47329e19ddb26fb1440b.

Here is some code that will reproduce the problem with the attached file. It executes as expected when I revert the above mentioned commit.

int main(int argc, char** argv)
{
	int err, i, streamIndex;
	AVFormatContext* fmtCtx;
	AVStream* stream;
	AVPacket pkt;

	av_register_all();

	fmtCtx = avformat_alloc_context();

	err = avformat_open_input(&fmtCtx, "test.mp3", NULL, NULL);
	if (err < 0)
	{
		fprintf(stderr, "Failed to open input\n");
		return 1;
	}

	avformat_find_stream_info(fmtCtx, NULL);

	streamIndex = -1;
	for (i = 0; i < fmtCtx->nb_streams; i++)
		if (fmtCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
		{
			streamIndex = i;
			break;
		}

	if (streamIndex < 0)
	{
		fprintf(stderr, "No audio stream\n");
		return 1;
	}

	stream = fmtCtx->streams[streamIndex];

	err = av_seek_frame(fmtCtx, streamIndex, stream->duration, AVSEEK_FLAG_BACKWARD);
	if (err < 0)	
	{
		fprintf(stderr, "Failed to seek stream\n");
		return 1;
	}

	
	av_init_packet(&pkt);
	pkt.data = NULL;
	pkt.size = 0;

	err = av_read_frame(fmtCtx, &pkt);
	if (err < 0)
	{
		char buf[1024];
		av_strerror(err, buf, sizeof(buf));

		fprintf(stderr, "Failed to read frame. Error: %d, %s\n", err, buf);
		return 1;
	}

	av_free_packet(&pkt);
	avformat_close_input(&fmtCtx);

	return 0;
}

Thanks,
Bill

Attachments (1)

test.mp3 (1.1 MB) - added by wadams 5 years ago.

Download all attachments as: .zip

Change History (3)

Changed 5 years ago by wadams

comment:1 Changed 5 years ago by cehoyos

  • Keywords regression added

Related to ticket #2590.

comment:2 Changed 5 years ago by michael

  • Reproduced by developer set
  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.