1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
| #include <iostream> extern "C" { #include "libavformat/avformat.h" #include "libavcodec/avcodec.h" #include "libavformat/avio.h" } #include <direct.h> using namespace std;
#define VIDEO_SAVE_PATH "yuv420_720x576_25fps.yuv"
#define AUDIO_SAVE_PATH "44.1khz_2ch_s16.pcm"
static char err_buf[128] = {0}; static char* av_get_err(int errnum) { av_strerror(errnum, err_buf, 128); return err_buf; }
void audio_decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile) { int data_size; int ret = avcodec_send_packet(dec_ctx, pkt); if(ret == AVERROR(EAGAIN)){printf("当前状态不接受输⼊,⽤户必须先⽤avcodec_receive_frame() 读取数据帧;\n");} else if(ret < 0){printf("提交包发生错误,err:%s,pkt->size=%d\n",av_get_err(ret),pkt->size);return;} while(ret >= 0) { ret = avcodec_receive_frame(dec_ctx, frame); if(ret == AVERROR(EAGAIN)||ret == AVERROR_EOF) return; else if(ret < 0){printf("解码过程有误!\n");return;} data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt); if(data_size < 0){printf("计算数据大小失败\n");return;} printf("ar-samplerate: %uHz\n", frame->sample_rate); printf("ac-channel: %u\n", frame->channels); printf("f-format: %u\n", frame->format); if (frame->format == AVSampleFormat::AV_SAMPLE_FMT_FLTP) { AVFrame* s16_frame = AllocS16PcmFrame(frame->channels, frame->nb_samples); if (!s16_frame) return; AudioResample audio_resampler; int ret = audio_resampler.InitFromFLTPToS16(frame->channels, frame->sample_rate, frame->channels, frame->sample_rate); if (ret < 0) { return; } ret = audio_resampler.ResampleFromFLTPToS16(frame, s16_frame); if (ret < 0) return; data_size = av_get_bytes_per_sample(AV_SAMPLE_FMT_S16); if (data_size < 0) { printf("计算数据大小失败\n"); return; } for (int i = 0; i < s16_frame->nb_samples * 2; i++) { fwrite(s16_frame->data[0] + data_size * i, 1, data_size, outfile); } FreePCMFrame(s16_frame); } else { for (int i = 0; i < frame->nb_samples; i++) { for (int ch = 0; ch < dec_ctx->channels; ch++) fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile); } } } }
void video_decode(AVCodecContext *dec_ctx, AVPacket *pkt,AVFrame *frame, FILE *outfile) { int data_size; int ret = avcodec_send_packet(dec_ctx, pkt); if(ret == AVERROR(EAGAIN)){printf("当前状态不接受输⼊,⽤户必须先⽤avcodec_receive_frame() 读取数据帧;\n");} else if(ret < 0){printf("提交包发生错误,err:%s,pkt->size=%d\n",av_get_err(ret),pkt->size);return;} while(ret >= 0) { ret = avcodec_receive_frame(dec_ctx, frame); if(ret == AVERROR(EAGAIN)||ret == AVERROR_EOF) return; else if(ret < 0){printf("解码过程有误!\n");return;} data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt); if(data_size < 0){printf("计算数据大小失败\n");return;} printf("width: %u\n", frame->width); printf("height: %u\n", frame->height); printf("format: %u\n", frame->format); for(int j=0; j<frame->height; j++) fwrite(frame->data[0] + j * frame->linesize[0], 1, frame->width, outfile); for(int j=0; j<frame->height/2; j++) fwrite(frame->data[1] + j * frame->linesize[1], 1, frame->width/2, outfile); for(int j=0; j<frame->height/2; j++) fwrite(frame->data[2] + j * frame->linesize[2], 1, frame->width/2, outfile); } }
void start_av_decode(AVFormatContext* fmt_context, int video_index, int audio_index) { AVPacket* pkt = av_packet_alloc(); AVFrame *decoded_frame = NULL; if(!decoded_frame){ if(!(decoded_frame = av_frame_alloc())){printf("不能分配帧内存\n");return;} } const AVCodec *v_codec; AVCodecContext *v_codec_ctx = NULL; enum AVCodecID video_codec_id = AV_CODEC_ID_H264;
v_codec = avcodec_find_decoder(video_codec_id); if(!v_codec){printf("找不到解码器!\n");return;} v_codec_ctx = avcodec_alloc_context3(v_codec); if(!v_codec_ctx){printf("无法分配解码器上下文!\n");return;} v_codec_ctx->pkt_timebase = fmt_context->streams[video_index]->time_base; avcodec_parameters_to_context(v_codec_ctx, fmt_context->streams[video_index]->codecpar); if(avcodec_open2(v_codec_ctx, v_codec, NULL) < 0){ printf("无法打开解码器\n");return; } FILE* v_out_file = fopen(VIDEO_SAVE_PATH, "wb");
const AVCodec *a_codec; AVCodecContext *a_codec_ctx = NULL; enum AVCodecID audio_codec_id = AV_CODEC_ID_AAC;
a_codec = avcodec_find_decoder(audio_codec_id); if(!a_codec){printf("找不到解码器!\n");return;} a_codec_ctx = avcodec_alloc_context3(a_codec); if(!a_codec_ctx){printf("无法分配解码器上下文!\n");return;} a_codec_ctx->pkt_timebase = fmt_context->streams[audio_index]->time_base; avcodec_parameters_to_context(a_codec_ctx, fmt_context->streams[audio_index]->codecpar); if(avcodec_open2(a_codec_ctx, a_codec, NULL) < 0){ printf("无法打开解码器\n");return; } FILE* a_out_file = fopen(AUDIO_SAVE_PATH, "wb");
do { int ret = av_read_frame(fmt_context, pkt); if (ret < 0){printf("av_read_frame end\n");break;} if (pkt->stream_index == video_index) { printf("A video packet\n"); video_decode(v_codec_ctx, pkt, decoded_frame, v_out_file); } else if (pkt->stream_index == audio_index) { printf("A audio packet\n"); audio_decode(a_codec_ctx, pkt, decoded_frame, a_out_file); } printf("pts:%lld\n", pkt->pts); printf("dts:%lld\n", pkt->dts); printf("size:%d\n", pkt->size); printf("pos:%lld\n", pkt->pos); printf("duration:%llf\n", pkt->duration * av_q2d(fmt_context->streams[pkt->stream_index]->time_base)); av_packet_unref(pkt); printf("\n"); } while (1);
pkt->data = NULL; pkt->size = 0; video_decode(v_codec_ctx, pkt, decoded_frame, v_out_file); audio_decode(a_codec_ctx, pkt, decoded_frame, a_out_file);
if (pkt){av_packet_free(&pkt);}
fclose(v_out_file); fclose(a_out_file); avcodec_free_context(&v_codec_ctx); avcodec_free_context(&a_codec_ctx); av_frame_free(&decoded_frame); av_packet_free(&pkt); }
void demux( const char* filepath ) { AVFormatContext* fmt_context = NULL; int video_index = -1; int audio_index = -1; int ret = avformat_open_input(&fmt_context, filepath, NULL, NULL); if (ret < 0) { char buf[1024] = { 0 }; av_strerror(ret, buf, sizeof(buf)); printf("open %s failed: %s\n", filepath, buf); return; } ret = avformat_find_stream_info(fmt_context, NULL); if (ret < 0) { char buf[1024] = { 0 }; av_strerror(ret, buf, sizeof(buf)); printf("avformat_find_stream_info %s failed: %s\n", filepath, buf); return; } av_dump_format(fmt_context, 0, filepath, 0); { printf("media->url: %s\n", fmt_context->url); printf("media->bit_rate: %lld\n", fmt_context->bit_rate); printf("media->nb_streams: %d\n", fmt_context->nb_streams); int total_seconds = (fmt_context->duration) / AV_TIME_BASE; int hours = total_seconds / 3600; int minutes = total_seconds % 3600 / 60; int seconds = total_seconds % 3600 % 60; printf("media->duration: %d-%d-%d\n", hours, minutes, seconds); printf("\n"); } for (auto i = 0; i < fmt_context->nb_streams; i++) { AVStream* is = fmt_context->streams[i]; if (AVMEDIA_TYPE_AUDIO == is->codecpar->codec_type) { audio_index = i; printf("stream index:%d\n", is->index); printf("audio sample_rate:%d Hz\n", is->codecpar->sample_rate); switch (is->codecpar->format){ case AV_SAMPLE_FMT_FLTP: printf("sampleformat:AV_SAMPLE_FMT_FLTP\n"); break; case AV_SAMPLE_FMT_S16P: printf("sampleformat:AV_SAMPLE_FMT_S16P\n"); break; default:break;} printf("audio channels: %d\n", is->codecpar->channels); switch (is->codecpar->codec_id){ case AV_CODEC_ID_AAC:printf("audio codec:AAC\n"); break; case AV_CODEC_ID_MP3:printf("audio codec:MP3\n"); break; default:printf("audio codec_id:%d\n", is->codecpar->codec_id);break;} if (is->duration != AV_NOPTS_VALUE) { int duration_audio_seconds = is->duration * av_q2d(is->time_base); printf("audio duration: %02d:%02d:%02d\n", duration_audio_seconds / 3600, (duration_audio_seconds % 3600) / 60, (duration_audio_seconds % 60)); } else { printf("duration unknown\n"); }; } else if (AVMEDIA_TYPE_VIDEO == is->codecpar->codec_type) { video_index = i; printf("stream index:%d\n", is->index); printf("video fps: %llf / s\n", av_q2d(is->avg_frame_rate)); switch (is->codecpar->codec_id) { case AV_CODEC_ID_MPEG4: printf("video codec:MPEG4\n"); break; case AV_CODEC_ID_H264: printf("video codec:H264\n"); break; default:printf("video codec_id:%d\n", is->codecpar->codec_id);break;} printf("video width:%d px height:%d px \n", is->codecpar->width, is->codecpar->height); if (is->duration != AV_NOPTS_VALUE) { int duration_video_seconds = is->duration * av_q2d(is->time_base); printf("video duration: %02d:%02d:%02d\n", duration_video_seconds / 3600, (duration_video_seconds % 3600) / 60, (duration_video_seconds % 60)); } else { printf("duration unknown\n"); }; } } printf("\n"); start_av_decode(fmt_context, video_index, audio_index); if (fmt_context){avformat_close_input(&fmt_context);} }
int main() { cout << "Hello World!" << endl; demux("sound_in_sync_test.mp4"); return 0; }
|