视频开发iOS Tips视频音频

ffmpeg 同步

2016-01-12  本文已影响1192人  zjunchao

音视频同步有三种方式

  1. 同步音频到视频
  2. 同步到外部时钟
  3. 同步视频到音频

同步视频到音频

二倍速度播放音频

属性:

/* indicates the current rate of playback; 0.0 means "stopped", 1.0 means "play at the natural rate of the current item" */
@property (nonatomic) float rate;

实例代码:

-(void)setPlaybackRate:(float)playbackRate
{
   _playbackRate = playbackRate;
   if (_player != nil && !isFloatZero(_player.rate)) {
       _player.rate = _playbackRate;
   }
}
-  (void)setPlaybackRate:(float)playbackRate
{
    if (fabsf(playbackRate - 1.0f) <= 0.000001) {
        UInt32 propValue = 1;
        AudioQueueSetProperty(_audioQueueRef, kAudioQueueProperty_TimePitchBypass, &propValue, sizeof(propValue));
        AudioQueueSetParameter(_audioQueueRef, kAudioQueueParam_PlayRate, 1.0f);
    } else {
        UInt32 propValue = 0;
        AudioQueueSetProperty(_audioQueueRef, kAudioQueueProperty_TimePitchBypass, &propValue, sizeof(propValue));
        AudioQueueSetParameter(_audioQueueRef, kAudioQueueParam_PlayRate, playbackRate);
    }
}

pts

视频读取pts

 ret = avcodec_decode_video2(d->avctx, frame, &got_frame, &d->pkt_temp);
                if (got_frame) {
                    ffp->stat.vdps = SDL_SpeedSamplerAdd(&ffp->vdps_sampler, FFP_SHOW_VDPS_AVCODEC, "vdps[avcodec]");
                    if (ffp->decoder_reorder_pts == -1) {
                        frame->pts = av_frame_get_best_effort_timestamp(frame);
                    } else if (ffp->decoder_reorder_pts) {
                        frame->pts = frame->pkt_pts;
                    } else {
                        frame->pts = frame->pkt_dts;
                    }
                }

int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame);

音频读取pts

函数实例:

 ret = avcodec_decode_audio4(d->avctx, frame, &got_frame, &d->pkt_temp);
                if (got_frame) {
                    AVRational tb = (AVRational){1, frame->sample_rate};
                    if (frame->pts != AV_NOPTS_VALUE)
                        frame->pts = av_rescale_q(frame->pts, d->avctx->time_base, tb);
                    else if (frame->pkt_pts != AV_NOPTS_VALUE)
                        frame->pts = av_rescale_q(frame->pkt_pts, av_codec_get_pkt_timebase(d->avctx), tb);
                    else if (d->next_pts != AV_NOPTS_VALUE)
                        frame->pts = av_rescale_q(d->next_pts, d->next_pts_tb, tb);
                    
                    if (frame->pts != AV_NOPTS_VALUE) {
                        d->next_pts = frame->pts + frame->nb_samples;
                        d->next_pts_tb = tb;
                    }
                }

音频一个AVPacket中只解析一个AVFrame, pkt_pts 可以当做真实的pts

流的时间基准

从某个音频或者视频流的时间基准中得到时间戳的 AVRational

AVRational av_codec_get_pkt_timebase (const AVCodecContext *avctx);

FrameQueue

typedef struct FrameQueue {
    Frame queue[FRAME_QUEUE_SIZE];
    int rindex;         // read index
    int windex;     // write index 
    int size;           // 大小
    int max_size;
    int keep_last;
    int rindex_shown; // read shown 已经读取的个数 
    SDL_mutex *mutex;
    SDL_cond *cond;
    PacketQueue *pktq;
} FrameQueue;

几个重要的函数

  1. synchronize_audio 音频clock校准函数
  2. video_refresh 视频的渲染调用
  3. compute_target_delay 计算音频和视频的delay
  4. update_video_pts 同步视频的时间戳
  5. audio_decode_frame 读取音频数据后同步音频时钟
  6. video_refresh_thread remaining_time决定每次休眠时间
上一篇 下一篇

猜你喜欢

热点阅读