YUV to RGB

Useful and helpful Windows specific command line samples and examples.
Post Reply
RedCat
Posts: 2
Joined: Sat Feb 24, 2018 5:18 pm

YUV to RGB

Post by RedCat » Sat Feb 24, 2018 5:32 pm

Hello to all users! Please tell me how to convert yuv to rgb32, doing so:

Code: Select all

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;
but for some reason after calling avpicture_fill the variable ImgConvContext: PSwsContext becomes equal to nil and SWS_Scale is not executed. How to fix it?
P.S. Sorry for my English I am from Uzbekistan

Post Reply