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
| #include <stdio.h> #include <stdlib.h> #include "libavcodec/avcodec.h" #include "libavfilter/avfilter.h" #include "libavformat/avformat.h" #include "libavutil/avutil.h" #include "libavutil/ffversion.h" #include "libswresample/swresample.h" #include "libswscale/swscale.h" #include "libpostproc/postprocess.h"
int main() { FILE *fp=fopen("result.yuv","w+b"); if(fp==NULL){ printf("Cannot open file.\n"); return -1; }
char filePath[] = "/home/jackey/Videos/Sample.mkv"; int videoStreamIndex = -1; int ret=0;
AVFormatContext *fmtCtx=NULL; AVPacket *pkt =NULL; AVCodecContext *codecCtx=NULL; AVCodecParameters *avCodecPara=NULL; const AVCodec *codec=NULL; AVFrame *yuvFrame = av_frame_alloc(); AVFrame *nv12Frame = av_frame_alloc();
unsigned char *out_buffer;
do{ fmtCtx = avformat_alloc_context(); if ((ret=avformat_open_input(&fmtCtx, filePath, NULL, NULL)) != 0) { printf("cannot open video file\n"); break; }
if ((ret=avformat_find_stream_info(fmtCtx, NULL)) < 0) { printf("cannot retrive video info\n"); break; }
for (unsigned int i = 0; i < fmtCtx->nb_streams; i++) { if (fmtCtx->streams[ i ]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } }
if (videoStreamIndex == -1) { printf("cannot find video stream\n"); break; }
av_dump_format(fmtCtx, 0, filePath, 0);
avCodecPara = fmtCtx->streams[ videoStreamIndex ]->codecpar; codec = avcodec_find_decoder(avCodecPara->codec_id); if (codec == NULL) { printf("cannot find decoder\n"); break; } codecCtx = avcodec_alloc_context3(codec); avcodec_parameters_to_context(codecCtx, avCodecPara); if (codecCtx == NULL) { printf("Cannot alloc context."); break; }
if ((ret=avcodec_open2(codecCtx, codec, NULL)) < 0) { printf("cannot open decoder\n"); break; }
int w=codecCtx->width; int h=codecCtx->height;
struct SwsContext *img_ctx = sws_getContext( codecCtx->width, codecCtx->height, codecCtx->pix_fmt, codecCtx->width, codecCtx->height, AV_PIX_FMT_NV12, SWS_BICUBIC, NULL, NULL, NULL);
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_NV12, codecCtx->width, codecCtx->height, 1); out_buffer = (unsigned char *)av_malloc(numBytes * sizeof(unsigned char));
pkt = av_packet_alloc(); av_new_packet(pkt, codecCtx->width * codecCtx->height); av_image_fill_arrays(nv12Frame->data, nv12Frame->linesize, out_buffer, AV_PIX_FMT_NV12, codecCtx->width, codecCtx->height, 1);
while (av_read_frame(fmtCtx, pkt) >= 0) { if (pkt->stream_index == videoStreamIndex){ if (avcodec_send_packet(codecCtx, pkt) == 0){ while (avcodec_receive_frame(codecCtx, yuvFrame) == 0){ sws_scale(img_ctx, (const uint8_t* const*)yuvFrame->data, yuvFrame->linesize, 0, h, nv12Frame->data, nv12Frame->linesize); fwrite(nv12Frame->data[0],1,w*h,fp); fwrite(nv12Frame->data[1],1,w*h/2,fp); } } } av_packet_unref(pkt); } }while(0); av_packet_free(&pkt); avcodec_close(codecCtx); avformat_close_input(&fmtCtx); avformat_free_context(fmtCtx); av_frame_free(&yuvFrame); av_frame_free(&nv12Frame);
av_free(out_buffer);
return ret; }
|