我想編碼YVU文件並將其保存爲jpg文件。但我不明白以下內容YUV到JPG編碼使用ffmpeg
1.爲什麼數據包大小是大小* 3。
av_new_packet(& PKT,大小* 3);爲什麼我們使用尺寸* 3/2`
2.In的fread。
如果(的fread(緩衝液,1,尺寸* 3/2,ptrInputFile)< = 0)`
3.how他們在這裏的填充數據
幀 - >數據[0] =緩衝器;
frame-> data [1] = buffer + siz;
frame-> data [2] = buffer + siz * 5/4;
代碼:
AVFormatContext *avFrameContext;
AVOutputFormat *avOutputFormat;
AVStream *avStream;
AVCodecContext *avCodecContext;
AVCodec *avCodec;
AVFrame *frame;
AVPacket pkt;
const char *output = "temp.jpg";
FILE *ptrInputFile;
const char *input = "cuc_view_480x272.yuv";
ptrInputFile = fopen(input ,"rb");
if(!ptrInputFile)
return -1;
avFrameContext = avformat_alloc_context();
avOutputFormat = av_guess_format("mjpeg", NULL, NULL);
if(!avOutputFormat)
return -1;
avFrameContext->oformat = avOutputFormat;
if(avio_open(&avFrameContext->pb ,output ,AVIO_FLAG_READ_WRITE)<0)
return -1;
avStream = avformat_new_stream(avFrameContext,NULL);
if(!avStream)
return -1;
avCodecContext = avStream->codec;
avCodecContext->codec_id = avOutputFormat->video_codec;
avCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
avCodecContext->pix_fmt = PIX_FMT_YUVJ420P;
avCodecContext->width = 480;
avCodecContext->height = 272;
avCodecContext->time_base.num = 1;
avCodecContext->time_base.den = 25;
avCodec = avcodec_find_encoder(avCodecContext->codec_id);
if(!avCodec)
return -1;
if(avcodec_open2(avCodecContext ,avCodec,NULL)<0)
return -1;
frame = av_frame_alloc();
int size = avpicture_get_size(PIX_FMT_YUVJ420P ,avCodecContext->width, avCodecContext->height);
uint8_t *buffer = (uint8_t*)av_malloc(size*sizeof(uint8_t));
avpicture_fill((AVPicture*)frame, buffer, avCodecContext->pix_fmt ,avCodecContext->width, avCodecContext->height);
//write header
avformat_write_header(avFrameContext, NULL);
int siz = avCodecContext->width*avCodecContext->height;
av_new_packet(&pkt,siz*3);
if(fread(buffer , 1, siz*3/2, ptrInputFile)<=0)
return -1;
frame->data[0] = buffer;
frame->data[1] = buffer + siz;
frame->data[2] = buffer + siz*5/4;
我剛更新了代碼。請看看,我正在使用C++ – Jeggu