Всем доброго дня, кто сталкивался с FFMPEG Подскажите пожалуйста, как конвертировать yuv в rgb32, делал так:
OpnFileName:=Panel1.Caption;
fmt_ctx:=nil;
Memo1.Clear;
Memo1.Lines.add('FileName = '+OpnFileName);
if avformat_open_input(fmt_ctx, PChar(OpnFileName), nil, nil) < 0 then
begin
Memo1.Lines.add('Could not open source file '+OpnFileName);
Exit;
end;
if avformat_find_stream_info(fmt_ctx , nil) < 0 then
begin
Memo1.Lines.add('Could not find stream information '+OpnFileName);
exit;
end;
Memo1.Lines.add('Video:');
video_index:= av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nil, 0);
if video_index < 0 then
begin
Memo1.Lines.add(Format('Could not find %s stream in input file ''%s''',
[string(av_get_media_type_string(AVMEDIA_TYPE_VIDEO)), OpnFileName]));
Memo1.Lines.add(inttostr(video_index));
end;
if video_index>=0 then
begin
pStreamVid:= PPtrIdx(fmt_ctx.streams, video_index);
dec_ctxVid := pStreamVid.codec;
avdecVid := avcodec_find_decoder(dec_ctxVid.codec_id);
if not Assigned(avdecVid) then
begin
Memo1.Lines.add(Format('Failed to find %s codec',
[string(av_get_media_type_string(AVMEDIA_TYPE_VIDEO))]));
end;
Memo1.Lines.add(avdecVid.long_name);
if avcodec_open2(dec_ctxVid,avdecVid,nil)<0 then
begin
Memo1.Lines.add(Format('Failed to decode %s With %s codec ',
[string (OpnFileName), (avdecVid.long_name)]));
end;
end;
Memo1.Lines.add('Audio:');
audio_index:= av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nil, 0);
if audio_index < 0 then
begin
Memo1.Lines.add(Format('Could not find %s stream in input file ''%s''',
[string(av_get_media_type_string(AVMEDIA_TYPE_AUDIO)), OpnFileName]));
Memo1.Lines.add(inttostr(audio_index));
end;
if audio_index>=0 then
begin
pStreamAud:= PPtrIdx(fmt_ctx.streams, audio_index);
dec_ctxAud := pStreamAud.codec;
avdecAud := avcodec_find_decoder(dec_ctxAud.codec_id);
if not Assigned(avdecAud) then
begin
Memo1.Lines.add(Format('Failed to find %s codec',
[string(av_get_media_type_string(AVMEDIA_TYPE_AUDIO))]));
end;
Memo1.Lines.add(avdecAud.long_name);
end;
ImgConvContext:=sws_getContext(pStreamVid.codec.width,pStreamVid.codec.height,pStreamVid.codec.pix_fmt,pStreamVid.codec.width,
pStreamVid.codec.height,AV_PIX_FMT_RGB32,SWS_BILINEAR,nil,nil,nil );
if ImgConvContext=nil then
Memo1.Lines.Add('Cannot create SWS context');
VidFrame:=avcodec_alloc_frame;
pict:=avcodec_alloc_frame;
numBytes:=avpicture_get_size(AV_PIX_FMT_RGB32, pStreamVid.codec.width, pStreamVid.codec.height);
If numBytes<0 then
Memo1.Lines.Add('Cannot avpicture_get_size');
buffer:=av_malloc(numBytes*sizeof(cardinal));
If Buffer=nil then
Memo1.Lines.Add('Cannot alloc buffer');
if avpicture_fill(PAVPicture(@pict), buffer, AV_PIX_FMT_RGB32, pStreamVid.codec.width, pStreamVid.codec.height)<0 then
Memo1.Lines.Add('Cannot fill picture');
if ImgConvContext=nil then
Memo1.Lines.Add('SWS context is lose');
while av_read_frame(fmt_ctx,@VidPacket)>=0 do
if VidPacket.stream_index=video_index then
begin
inc(i);
avcodec_decode_video2(dec_ctxVid,VidFrame,FrameFinished,@VidPacket);
if FrameFinished>0 then
begin
if ImgConvContext<>nil then
sws_scale(ImgConvContext, @VidFrame.data, @VidFrame.linesize,0, pStreamVid.codec.height,@pict.data,@pict.linesize);
end;
If @pict.data[0]<>nil then
SaveFrame(@pict,pStreamVid.codec.width,pStreamVid.codec.height);
end;
но почему-то после вызова avpicture_fill переменная ImgConvContext:PSwsContext становится равной nil и SWS_Scale не выполняется. Подскажите как быть?