[libav-api] av_read_frame() fails after seeking

Ronald S. Bultje rsbultje at gmail.com
Fri Jun 3 17:13:09 CEST 2011


Hi,

On Fri, Jun 3, 2011 at 8:00 AM, wecing <w3cing at gmail.com> wrote:
> Here is my code:
>
> void SA_seek(SAContext *sa_ctx, double delta)
> {
>      SDL_mutexP(sa_ctx->decode_lock);
>      int64_t pos = (av_gettime() / 1000000.0 + delta) * AV_TIME_BASE;
>      int seek_flags = delta < 0 ? AVSEEK_FLAG_BACKWARD : 0;
>      int stream_index = sa_ctx->v_stream;
>      int64_t seek_target = av_rescale_q(pos, AV_TIME_BASE_Q,
>                                         sa_ctx->video_st->time_base);
>      if(av_seek_frame(sa_ctx->avfmt_ctx_ptr, stream_index, seek_target,
> seek_flags) < 0)
>           fprintf(stderr, "Error while seeking!\n"); // FIXME
>      else
>      {
>           void *ptr;
>           while((ptr = SAQ_pop(sa_ctx->vq_ctx)) != NULL)
>           {
>                av_free(((SAVideoPacket *)ptr)->frame_ptr);
>                free(ptr);
>           }
>           while((ptr = SAQ_pop(sa_ctx->aq_ctx)) != NULL)
>           {
>                av_free(((SAAudioPacket *)ptr)->abuffer);
>                free(ptr);
>           }
>
>           avcodec_flush_buffers(sa_ctx->a_codec_ctx);
>           avcodec_flush_buffers(sa_ctx->v_codec_ctx);
>      }
>      SDL_mutexV(sa_ctx->decode_lock);
> }
>
> and this, in main():
>
> while(SDL_PollEvent(&event))
>      if(event.type == SDL_KEYDOWN)
>      {
>           double delta;
>           switch(event.key.keysym.sym)
>           {
>           case SDLK_LEFT:
>                delta = -10.0;
>                break;
>           case SDLK_RIGHT:
>                delta = 10.0;
>                break;
>           case SDLK_UP:
>                delta = -60.0;
>                break;
>           case SDLK_DOWN:
>                delta = 60.0;
>                break;
>           default:
>                goto IGNORE_KEY;
>           }
>
>           SA_seek(sa_ctx, delta);
>           SDL_PauseAudio(0);
>
>      IGNORE_KEY:;
>      }
>
> After calling SA_seek(), my program will call av_read_frame() to get a new
> AVPacket for decoding.
>
> but it just refuses to work, returning a valus less than zero. :-P

What type of file are you reading?

Ronald


More information about the libav-api mailing list