ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

FFmpeg 之音视频解码与音视频同步(二),Android社招面经分享

2021-09-10 13:00:30  阅读:150  来源: 互联网

标签:社招 FFmpeg frame 音视频 sample env av audio out


//获取视频文件信息,例如得到视频的宽高

//第二个参数是一个字典,表示你需要获取什么信息,比如视频的元数据

if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {

    LOGE("%s", "无法获取视频文件信息");

    return;

}



//获取视频流的索引位置

//遍历所有类型的流(音频流、视频流、字幕流),找到视频流

int v_stream_idx = -1;

int i = 0;

//number of streams

for (; i < pFormatCtx->nb_streams; i++) {

    //流的类型

    if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {

        v_stream_idx = i;

        break;

    }

}



if (v_stream_idx == -1) {

    LOGE("%s", "找不到视频流\n");

    return;

}



//获取视频流中的编解码上下文

AVCodecContext *pCodecCtx = pFormatCtx->streams[v_stream_idx]->codec;



//根据编解码上下文中的编码 id 查找对应的解码器

AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);



if (pCodec == NULL) {

    LOGE("%s", "找不到解码器,或者视频已加密\n");

    return;

}



//打开解码器,解码器有问题(比如说我们编译FFmpeg的时候没有编译对应类型的解码器)

if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {

    LOGE("%s", "解码器无法打开\n");

    return;

}



//准备读取

//AVPacket用于存储一帧一帧的压缩数据(H264)

//缓冲区,开辟空间

AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));



//AVFrame用于存储解码后的像素数据(YUV)

//内存分配

AVFrame *yuv_frame = av_frame_alloc();

AVFrame *rgb_frame = av_frame_alloc();



int got_picture, ret;

int frame_count = 0;



//窗体

ANativeWindow *pWindow = ANativeWindow_fromSurface(env, surface);

//绘制时的缓冲区

ANativeWindow_Buffer out_buffer;



//一帧一帧的读取压缩数据

while (av_read_frame(pFormatCtx, packet) >= 0) {

    //只要视频压缩数据(根据流的索引位置判断)

    if (packet->stream_index == v_stream_idx) {

        //7.解码一帧视频压缩数据,得到视频像素数据

        ret = avcodec_decode_video2(pCodecCtx, yuv_frame, &got_picture, packet);

        if (ret < 0) {

            LOGE("%s", "解码错误");

            return;

        }



        //为0说明解码完成,非0正在解码

        if (got_picture) {



            //lock window

            //设置缓冲区的属性:宽高、像素格式(需要与Java层的格式一致)

            ANativeWindow_setBuffersGeometry(pWindow, pCodecCtx->width, pCodecCtx->height,

                                             WINDOW_FORMAT_RGBA_8888);

            ANativeWindow_lock(pWindow, &out_buffer, NULL);



            //初始化缓冲区

            //设置属性,像素格式、宽高

            //rgb_frame的缓冲区就是Window的缓冲区,同一个,解锁的时候就会进行绘制

            avpicture_fill((AVPicture *) rgb_frame, out_buffer.bits, AV_PIX_FMT_RGBA,

                           pCodecCtx->width,

                           pCodecCtx->height);



            //YUV格式的数据转换成RGBA 8888格式的数据, FFmpeg 也可以转换,但是存在问题,使用libyuv这个库实现

            I420ToARGB(yuv_frame->data[0], yuv_frame->linesize[0],

                       yuv_frame->data[2], yuv_frame->linesize[2],

                       yuv_frame->data[1], yuv_frame->linesize[1],

                       rgb_frame->data[0], rgb_frame->linesize[0],

                       pCodecCtx->width, pCodecCtx->height);



            //3、unlock window

            ANativeWindow_unlockAndPost(pWindow);



            frame_count++;

            LOGI("解码绘制第%d帧", frame_count);

        }

    }



    //释放资源

    av_free_packet(packet);

}



av_frame_free(&yuv_frame);

avcodec_close(pCodecCtx);

avformat_free_context(pFormatCtx);

(*env)->ReleaseStringUTFChars(env, input_, input);

}

#include “libswresample/swresample.h”

#define MAX_AUDIO_FRME_SIZE 48000 * 4

//音频解码(重采样)

JNIEXPORT void JNICALL

Java_com_haohao_ffmpeg_AVUtils_audioDecode(JNIEnv *env, jclass type, jstring input_,

                                       jstring output_) {

//访问静态方法

jmethodID mid = (*env)->GetStaticMethodID(env, type, "onNativeCallback", "()V");

const char *input = (*env)->GetStringUTFChars(env, input_, 0);

const char *output = (*env)->GetStringUTFChars(env, output_, 0);



//注册组件

av_register_all();

AVFormatContext *pFormatCtx = avformat_alloc_context();

//打开音频文件

if (avformat_open_input(&pFormatCtx, input, NULL, NULL) != 0) {

    LOGI("%s", "无法打开音频文件");

    return;

}

//获取输入文件信息

if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {

    LOGI("%s", "无法获取输入文件信息");

    return;

}

//获取音频流索引位置

int i = 0, audio_stream_idx = -1;

for (; i < pFormatCtx->nb_streams; i++) {

    if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {

        audio_stream_idx = i;

        break;

    }

}



//获取解码器

AVCodecContext *codecCtx = pFormatCtx->streams[audio_stream_idx]->codec;

AVCodec *codec = avcodec_find_decoder(codecCtx->codec_id);

if (codec == NULL) {

    LOGI("%s", "无法获取解码器");

    return;

}

//打开解码器

if (avcodec_open2(codecCtx, codec, NULL) < 0) {

    LOGI("%s", "无法打开解码器");

    return;

}

//压缩数据

AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));

//解压缩数据

AVFrame *frame = av_frame_alloc();

//frame->16bit 44100 PCM 统一音频采样格式与采样率

SwrContext *swrCtx = swr_alloc();



//重采样设置参数

//输入的采样格式

enum AVSampleFormat in_sample_fmt = codecCtx->sample_fmt;

//输出采样格式16bit PCM

enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;

//输入采样率

int in_sample_rate = codecCtx->sample_rate;

//输出采样率

int out_sample_rate = 44100;

//获取输入的声道布局

//根据声道个数获取默认的声道布局(2个声道,默认立体声stereo)

//av_get_default_channel_layout(codecCtx->channels);

uint64_t in_ch_layout = codecCtx->channel_layout;

//输出的声道布局(立体声)

uint64_t out_ch_layout = AV_CH_LAYOUT_STEREO;



swr_alloc_set_opts(swrCtx,

                   out_ch_layout, out_sample_fmt, out_sample_rate,

                   in_ch_layout, in_sample_fmt, in_sample_rate,

                   0, NULL);

swr_init(swrCtx);



//输出的声道个数

int out_channel_nb = av_get_channel_layout_nb_channels(out_ch_layout);



//重采样设置参数



//位宽16bit 采样率 44100HZ 的 PCM 数据

uint8_t *out_buffer = (uint8_t *) av_malloc(MAX_AUDIO_FRME_SIZE);



FILE *fp_pcm = fopen(output, "wb");



int got_frame = 0, index = 0, ret;

//不断读取压缩数据

while (av_read_frame(pFormatCtx, packet) >= 0) {

    //解码

    ret = avcodec_decode_audio4(codecCtx, frame, &got_frame, packet);



    if (ret < 0) {

        LOGI("%s", "解码完成");

    }

    //解码一帧成功

    if (got_frame > 0) {

        LOGI("解码:%d", index++);

        swr_convert(swrCtx, &out_buffer, MAX_AUDIO_FRME_SIZE, frame->data, frame->nb_samples);

        //获取sample的size

        int out_buffer_size = av_samples_get_buffer_size(NULL, out_channel_nb,

                                                         frame->nb_samples, out_sample_fmt, 1);

        fwrite(out_buffer, 1, out_buffer_size, fp_pcm);

    }



    av_free_packet(packet);

}



fclose(fp_pcm);

av_frame_free(&frame);

av_free(out_buffer);



swr_free(&swrCtx);

avcodec_close(codecCtx);

avformat_close_input(&pFormatCtx);



(*env)->ReleaseStringUTFChars(env, input_, input);

(*env)->ReleaseStringUTFChars(env, output_, output);

//通知 Java 层解码完成

(*env)->CallStaticVoidMethod(env, type, mid);

}

JNIEXPORT void JNICALL

Java_com_haohao_ffmpeg_AVUtils_audioPlay(JNIEnv *env, jclass type, jstring input_) {

const char *input = (*env)->GetStringUTFChars(env, input_, 0);

LOGI("%s", "sound");

//注册组件

av_register_all();

AVFormatContext *pFormatCtx = avformat_alloc_context();

//打开音频文件

if (avformat_open_input(&pFormatCtx, input, NULL, NULL) != 0) {

    LOGI("%s", "无法打开音频文件");

    return;

}

//获取输入文件信息

if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {

    LOGI("%s", "无法获取输入文件信息");

    return;

}

//获取音频流索引位置

int i = 0, audio_stream_idx = -1;

for (; i < pFormatCtx->nb_streams; i++) {

    if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {

        audio_stream_idx = i;

        break;

    }

}



//获取解码器

AVCodecContext *codecCtx = pFormatCtx->streams[audio_stream_idx]->codec;

AVCodec *codec = avcodec_find_decoder(codecCtx->codec_id);

if (codec == NULL) {

    LOGI("%s", "无法获取解码器");

    return;

}

//打开解码器

if (avcodec_open2(codecCtx, codec, NULL) < 0) {

    LOGI("%s", "无法打开解码器");

    return;

}

//压缩数据

AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));

//解压缩数据

AVFrame *frame = av_frame_alloc();

//frame->16bit 44100 PCM 统一音频采样格式与采样率

SwrContext *swrCtx = swr_alloc();



//输入的采样格式

enum AVSampleFormat in_sample_fmt = codecCtx->sample_fmt;

//输出采样格式16bit PCM

enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;

//输入采样率

int in_sample_rate = codecCtx->sample_rate;

//输出采样率

int out_sample_rate = in_sample_rate;

//获取输入的声道布局

//根据声道个数获取默认的声道布局(2个声道,默认立体声stereo)

//av_get_default_channel_layout(codecCtx->channels);

uint64_t in_ch_layout = codecCtx->channel_layout;

//输出的声道布局(立体声)

uint64_t out_ch_layout = AV_CH_LAYOUT_STEREO;



swr_alloc_set_opts(swrCtx,

                   out_ch_layout, out_sample_fmt, out_sample_rate,

                   in_ch_layout, in_sample_fmt, in_sample_rate,

                   0, NULL);

swr_init(swrCtx);



//输出的声道个数

int out_channel_nb = av_get_channel_layout_nb_channels(out_ch_layout);

//AudioTrack对象

jmethodID create_audio_track_mid = (*env)->GetStaticMethodID(env, type, "createAudioTrack",

                                                             "(II)Landroid/media/AudioTrack;");

jobject audio_track = (*env)->CallStaticObjectMethod(env, type, create_audio_track_mid,

                                                     out_sample_rate, out_channel_nb);



//调用AudioTrack.play方法

jclass audio_track_class = (*env)->GetObjectClass(env, audio_track);

jmethodID audio_track_play_mid = (*env)->GetMethodID(env, audio_track_class, "play", "()V");

jmethodID audio_track_stop_mid = (*env)->GetMethodID(env, audio_track_class, "stop", "()V");

(*env)->CallVoidMethod(env, audio_track, audio_track_play_mid);



//AudioTrack.write

jmethodID audio_track_write_mid = (*env)->GetMethodID(env, audio_track_class, "write",

                                                      "([BII)I");

//16bit 44100 PCM 数据

uint8_t *out_buffer = (uint8_t *) av_malloc(MAX_AUDIO_FRME_SIZE);



int got_frame = 0, index = 0, ret;

//不断读取压缩数据

while (av_read_frame(pFormatCtx, packet) >= 0) {

    //解码音频类型的Packet

    if (packet->stream_index == audio_stream_idx) {

        //解码

        ret = avcodec_decode_audio4(codecCtx, frame, &got_frame, packet);



        if (ret < 0) {

            LOGI("%s", "解码完成");

        }

        //解码一帧成功

        if (got_frame > 0) {

            LOGI("解码:%d", index++);

            swr_convert(swrCtx, &out_buffer, MAX_AUDIO_FRME_SIZE,

                        (const uint8_t **) frame->data, frame->nb_samples);

            //获取sample的size

            int out_buffer_size = av_samples_get_buffer_size(NULL, out_channel_nb,

                                                             frame->nb_samples, out_sample_fmt,

                                                             1);



            //out_buffer缓冲区数据,转成byte数组

            jbyteArray audio_sample_array = (*env)->NewByteArray(env, out_buffer_size);

            jbyte *sample_bytep = (*env)->GetByteArrayElements(env, audio_sample_array, NULL);

            //out_buffer的数据复制到sampe_bytep

            memcpy(sample_bytep, out_buffer, out_buffer_size);

            //同步

            (*env)->ReleaseByteArrayElements(env, audio_sample_array, sample_bytep, 0);



            //AudioTrack.write PCM数据

            (*env)->CallIntMethod(env, audio_track, audio_track_write_mid,

                                  audio_sample_array, 0, out_buffer_size);

            //释放局部引用

            (*env)->DeleteLocalRef(env, audio_sample_array);

        }

    }

    av_free_packet(packet);

}



(*env)->CallVoidMethod(env, audio_track, audio_track_stop_mid);



av_frame_free(&frame);

av_free(out_buffer);



swr_free(&swrCtx);

avcodec_close(codecCtx);

avformat_close_input(&pFormatCtx);



(*env)->ReleaseStringUTFChars(env, input_, input);

}




CMakeLists.txt



cmake_minimum_required(VERSION 3.4.1)

include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)

set(jnilibs “${CMAKE_SOURCE_DIR}/src/main/jniLibs”)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI})

add_library( # Sets the name of the library.

         native-lib



         # Sets the library as a shared library.

         SHARED



         # Provides a relative path to your source file(s).

         src/main/cpp/native-lib.c)

添加 FFmpeg 的 8 个函数库和 yuvlib 库

add_library(avutil-54 SHARED IMPORTED )

set_target_properties(avutil-54 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libavutil-54.so”)

add_library(swresample-1 SHARED IMPORTED )

set_target_properties(swresample-1 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libswresample-1.so”)

add_library(avcodec-56 SHARED IMPORTED )

set_target_properties(avcodec-56 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libavcodec-56.so”)

add_library(avformat-56 SHARED IMPORTED )

set_target_properties(avformat-56 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libavformat-56.so”)

add_library(swscale-3 SHARED IMPORTED )

set_target_properties(swscale-3 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libswscale-3.so”)

add_library(postproc-53 SHARED IMPORTED )

set_target_properties(postproc-53 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libpostproc-53.so”)

add_library(avfilter-5 SHARED IMPORTED )

set_target_properties(avfilter-5 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libavfilter-5.so”)

add_library(avdevice-56 SHARED IMPORTED )

set_target_properties(avdevice-56 PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libavdevice-56.so”)

add_library(yuv SHARED IMPORTED )

set_target_properties(yuv PROPERTIES IMPORTED_LOCATION “ j n i l i b s / {jnilibs}/ jnilibs/{ANDROID_ABI}/libyuv.so”)

find_library( # Sets the name of the path variable.

          log-lib



          # Specifies the name of the NDK library that

          # you want CMake to locate.

          log )

#找到 Android 系统 Window 绘制相关的库

find_library(

        android-lib

        android

        )

target_link_libraries( native-lib

                   ${log-lib} 

                   ${android-lib} 

                   avutil-54 

                   swresample-1

                   avcodec-56

                   avformat-56

                   swscale-3

                   postproc-53

                   avfilter-5

                   avdevice-56

                   yuv) 



PS:



1.  注意添加文件读写权限。



二、音视频同步原理与实现

============



2.1、原理

------



如果简单的按照音频的采样率与视频的帧率去播放,由于机器运行速度,解码效率等种种造成时间差异的因素影响,很难同步,音视频时间差将会呈现线性增长。所以要做音视频的同步,有三种方式:



1.参考一个外部时钟,将音频与视频同步至此时间。我首先想到这种方式,但是并不好,由于某些生物学的原理,人对声音的变化比较敏感,但是对视觉变化不太敏感。所以频繁的去调整声音的播放会有些刺耳或者杂音吧影响用户体验。(ps:顺便科普生物学知识,自我感觉好高大上\_)。



2.以视频为基准,音频去同步视频的时间。不采用,理由同上。



3.以音频为基准,视频去同步音频的时间。 所以这个办法了。



所以,原理就是以音频时间为基准,判断视频快了还是慢了,从而调整视频速度。其实是一个动态的追赶与等待的过程。



2.2、一些概念

--------



音视频中都有DTS与PTS。



DTS ,Decoding Time Stamp,解码时间戳,告诉解码器packet的解码顺序。  

PTS ,Presentation Time Stamp,显示时间戳,指示从packet中解码出来的数据的显示顺序。  

音频中二者是相同的,但是视频由于B帧(双向预测)的存在,会造成解码顺序与显示顺序并不相同,也就是视频中DTS与PTS不一定相同。



时间基 :看FFmpeg源码



/**

 * This is the fundamental unit of time (in seconds) in terms

 * of which frame timestamps are represented. For fixed-fps content,

 * timebase should be 1/framerate and timestamp increments should be

 * identically 1.

 * This often, but not always is the inverse of the frame rate or field rate

 * for video.

 * - encoding: MUST be set by user.

 * - decoding: the use of this field for decoding is deprecated.

 *             Use framerate instead.

 */

AVRational time_base; 

/**

 * rational number numerator/denominator

 */

typedef struct AVRational{

    int num; ///< numerator

    int den; ///< denominator

} AVRational; 

```



个人理解,其实就是ffmpeg中的用分数表示时间单位,num为分子,den为分母。并且ffmpeg提供了计算方法:



```

/**

 * Convert rational to double.

 * @param a rational to convert

 * @return (double) a

 */

static inline double av_q2d(AVRational a){

    return a.num / (double) a.den;

} 

```



所以 视频中某帧的显示时间 计算方式为(单位为妙):



```

time = pts * av_q2d(time_base); 

```



2.3、同步代码

--------



**1、 音频部分**  

clock 为音频的播放时长(从开始到当前的时间)



```

if (packet->pts != AV_NOPTS_VALUE) {

            audio->clock = av_q2d(audio->time_base) * packet->pts;

 } 

```



然后加上此packet中数据需要播放的时间



```

double time = datalen/((double) 44100 *2 * 2);

audio->clock = audio->clock +time; 

```



datalen为数据长度。采样率为44100,采样位数为16,通道数为2。所以 数据长度 / 每秒字节数。



ps:此处计算方式不是很完美,有很多问题,回头研究在再补上。



**2、 视频部分**  

先定义几个值:



```

 double  last_play  //上一帧的播放时间

    ,play             //当前帧的播放时间

    , last_delay    // 上一次播放视频的两帧视频间隔时间

    ,delay         //两帧视频间隔时间

    ,audio_clock //音频轨道 实际播放时间

    ,diff   //音频帧与视频帧相差时间

    ,sync_threshold //合理的范围

    ,start_time  //从第一帧开始的绝对时间

    ,pts

    ,actual_delay//真正需要延迟时间

    start_time = av_gettime() / 1000000.0; 

// 获取pts

    if ((pts = av_frame_get_best_effort_timestamp(frame)) == AV_NOPTS_VALUE) {

        pts = 0;

    }

    play = pts * av_q2d(vedio->time_base);

// 纠正时间

    play = vedio->synchronize(frame, play);

    delay = play - last_play;

    if (delay <= 0 || delay > 1) {

        delay = last_delay;

    }

    audio_clock = vedio->audio->clock;

    last_delay = delay;

    last_play = play;

//音频与视频的时间差

    diff = vedio->clock - audio_clock;

// 在合理范围外 才会延迟 加快

    sync_threshold = (delay > 0.01 ? 0.01 : delay);



    if (fabs(diff) < 10) {

        if (diff <= -sync_threshold) {

            delay = 0;

        } else if (diff >= sync_threshold) {

            delay = 2 * delay;

        }

    }

    start_time += delay;

    actual_delay = start_time - av_gettime() / 1000000.0;

    if (actual_delay < 0.01) {

        actual_delay = 0.01;

    }

// 休眠时间 ffmpeg 建议这样写 为什么 要这样写 有待研究

    av_usleep(actual_delay * 1000000.0 + 6000); 



纠正play (播放时间)的方法 repeat\_pict / (2 \* fps) 是ffmpeg注释里教的



synchronize(AVFrame *frame, double play) {

//clock是当前播放的时间位置

if (play != 0)

    clock=play;

else //pst为0 则先把pts设为上一帧时间

    play = clock;

//可能有pts为0 则主动增加clock

//需要求出扩展延时:

double repeat_pict = frame->repeat_pict;

//使用AvCodecContext的而不是stream的

double frame_delay = av_q2d(codec->time_base);

//fps 

double fps = 1 / frame_delay;

//pts 加上 这个延迟 是显示时间  

double extra_delay = repeat_pict / (2 * fps);

double delay = extra_delay + frame_delay;

clock += delay;

总结

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的Android开发中高级必知必会核心笔记,共计2968页PDF、58w字,囊括Android开发648个知识点,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。

**[CodeChina开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》](

)**

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。

虽然面试失败了,但我也不会放弃入职字节跳动的决心的!建议大家面试之前都要有充分的准备,顺顺利利的拿到自己心仪的offer。

本文已被腾讯CODING开源托管项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录,自学资源及系列文章持续更新中…
是ffmpeg注释里教的


synchronize(AVFrame *frame, double play) {

    //clock是当前播放的时间位置

    if (play != 0)

        clock=play;

    else //pst为0 则先把pts设为上一帧时间

        play = clock;

    //可能有pts为0 则主动增加clock

    //需要求出扩展延时:

    double repeat_pict = frame->repeat_pict;

    //使用AvCodecContext的而不是stream的

    double frame_delay = av_q2d(codec->time_base);

    //fps 

    double fps = 1 / frame_delay;

    //pts 加上 这个延迟 是显示时间  

    double extra_delay = repeat_pict / (2 * fps);

    double delay = extra_delay + frame_delay;

    clock += delay;


### 总结

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的Android开发中高级必知必会核心笔记,共计2968页PDF、58w字,囊括Android开发648个知识点,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。

[外链图片转存中...(img-cRAPhgE4-1631249000198)]

**[CodeChina开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》](

)**

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。

虽然面试失败了,但我也不会放弃入职字节跳动的决心的!建议大家面试之前都要有充分的准备,顺顺利利的拿到自己心仪的offer。


> **本文已被[腾讯CODING开源托管项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》](https://ali1024.coding.net/public/P7/Android/git)收录,自学资源及系列文章持续更新中...**

标签:社招,FFmpeg,frame,音视频,sample,env,av,audio,out
来源: https://blog.csdn.net/m0_61072577/article/details/120219699

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有