[Libav-user] audio-problem when playing multiple video segments

GeraldH mirkogheinkel at gmail.com
Mon May 21 12:19:25 CEST 2012


hi all,

i am using an updated (latest ffmpeg, SDL 1.2.15, MSVC++) version of
Dranger's tutorial and basically, it works fine.
now, i started to extend it for to play multiple 10-seconds video segments
(gapless), what also works out as long as i don't fill the audio queue. 
but if i do so, as soon it switches to the second segment, i just get some
fast techno-style noise, but video is still running.

here is the code of the main loop:

main(...)
{
...
	while (1)
	{
		while(av_read_frame(pFormatCtx, &packet) >= 0) 
		{
			if(packet.stream_index == videoStream) 
			{
				avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

				if(frameFinished) 
				{
					SDL_LockYUVOverlay(bmp);

					AVPicture pict;
					pict.data[0] = bmp->pixels[0];
					pict.data[1] = bmp->pixels[2];
					pict.data[2] = bmp->pixels[1];

					pict.linesize[0] = bmp->pitches[0];
					pict.linesize[1] = bmp->pitches[2];
					pict.linesize[2] = bmp->pitches[1];
					
					sws_scale(ctx, pFrame->data, pFrame->linesize, 0, h,pict.data,
pict.linesize);
					//av_picture_copy( (AVPicture*)&pict, (AVPicture*)pFrame,
pCodecCtx->pix_fmt, w, h);

					SDL_UnlockYUVOverlay(bmp);

					rect.x = 0;
					rect.y = 0;
					rect.w = w;
					rect.h = h;
					SDL_DisplayYUVOverlay(bmp, &rect);
					SDL_Delay(40);
				}
			}
			else if(packet.stream_index == audioStream) 
			{
				packet_queue_put(&audioq, &packet);
			} 
			else 
			{
				av_free_packet(&packet);
			}

			SDL_PollEvent(&event);
			switch(event.type) 
			{
				case SDL_QUIT:
					quit = 1;
					break;
				case SDL_KEYUP:
					if (event.key.keysym.sym == SDLK_ESCAPE)
						quit = 1;
					break;
				default:
					break;
			}

			if (quit)
			{
				SDL_Quit();
				exit(0);
			}
		}

		av_free(buffer);
		av_free(pFrameRGB);
		av_free(pFrame);
		avcodec_close(pCodecCtx);
		av_close_input_file(pFormatCtx);
		pFormatCtx = avformat_alloc_context();

		fileNo++;

		if (fileNo == 10)
			break;


		//switch to the next segment file
		char szFilename[5];
		sprintf(szFilename, "%d.asf", fileNo);
		if(avformat_open_input(&pFormatCtx, szFilename, NULL, NULL)!=0)
			return -1; // Couldn't open file

		//initializing audio and video as done before entering the while loop...
		aCodecCtx = pFormatCtx->streams[audioStream]->codec;
		wanted_spec.freq = aCodecCtx->sample_rate;
		wanted_spec.format = AUDIO_S16SYS;
		wanted_spec.channels = aCodecCtx->channels;
		wanted_spec.silence = 0;
		wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
		wanted_spec.callback = audio_callback;
		wanted_spec.userdata = aCodecCtx;

		SDL_OpenAudio(&wanted_spec, &spec); 
		aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
		avcodec_open(aCodecCtx, aCodec);

		packet_queue_init(&audioq);
		SDL_PauseAudio(0);

		pCodecCtx=pFormatCtx->streams[videoStream]->codec;
		pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
		avcodec_open(pCodecCtx, pCodec);
		pFrame=avcodec_alloc_frame();
		pFrameRGB=avcodec_alloc_frame();
		//numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
		buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
	}

...
}

int packet_queue_put(PacketQueue *q, AVPacket *pkt) 
{
	AVPacketList *pkt1;
	if(av_dup_packet(pkt) < 0)  
		return -1;
	
	//check mirko:
	pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
	if (!pkt1)
		return -1;

	pkt1->pkt = *pkt;
	pkt1->next = NULL;

	SDL_LockMutex(q->mutex);

	if (!q->last_pkt)
		q->first_pkt = pkt1;
	else
		q->last_pkt->next = pkt1;
	q->last_pkt = pkt1;
	q->nb_packets++;
	q->size += pkt1->pkt.size;

	SDL_CondSignal(q->cond);
	SDL_UnlockMutex(q->mutex);
	return 0;
}

can anybody give me a hint whats going wrong ?
- or -
is anyone interested in a freelance job ?
todo's would be:
- based on the code above, gapless playing of video/audio of multiple video
segments.
- video output on any given windows form (e.g. a panel) by using 
   SDL_GetWMInfo(SDL_SysWMinfo *info); // SDL_syswm.h
- other things like pause, stop, play, volume control

i will appreciate any question about that!

regards,

Gerald




--
View this message in context: http://libav-users.943685.n4.nabble.com/audio-problem-when-playing-multiple-video-segments-tp4647937.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list