0

Всем доброго дня, кто сталкивался с 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 не выполняется. Подскажите как быть?

1 ответ 1

0

Проблема решилась таким образом:

 OpnFileName:=Panel1.Caption;
 fmt_ctx:=nil;
 Memo1.Clear;
 Memo1.Lines.add('FileName = '+OpnFileName);
 bmp := TBitmap.Create;
 try
 if avformat_open_input(fmt_ctx, PANSIChar(AnsiString(OpnFileName)), nil, nil) < 0 then
 begin
  Memo1.Lines.add('Could not open source file '+PANSIChar(AnsiString(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;
Memo1.Lines.add(inttostr(dec_ctxVid.width)+'x'+  inttostr(dec_ctxVid.height));
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;
  VidFrame:=avcodec_alloc_frame;

// Allocate an AVFrame structure
pict:=avcodec_alloc_frame();
if not assigned(pict) then
begin
  Memo1.Lines.add('Could not allocate AVFrame structure');
  exit;
end;
SetStretchBltMode(PaintBox1.Canvas.Handle,COLORONCOLOR);
// Determine required buffer size and allocate buffer
numBytes:=avpicture_get_size(PIX_FMT_RGB24, dec_ctxVid.width,  dec_ctxVid.height);
buffer:=av_malloc(numBytes*sizeof(cardinal));

ImgConvContext :=
sws_getContext
(
    dec_ctxVid.width,
    dec_ctxVid.height,
    dec_ctxVid.pix_fmt,
    dec_ctxVid.width,
    dec_ctxVid.height,
    PIX_FMT_RGB32,
    SWS_BILINEAR,
    nil,
    nil,
    nil
);

// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill(PAVPicture(pict), buffer, PIX_FMT_RGB32, dec_ctxVid.width, dec_ctxVid.height);
bmp.PixelFormat := pf32bit;
bmp.Width := dec_ctxVid.width;
bmp.Height := dec_ctxVid.height;
// Read frames and save first five frames to disk
i:=0;
while(av_read_frame(fmt_ctx, @VidPacket)>=0) do
begin
  // Is this a packet from the video stream?
  if(VidPacket.stream_index=video_index) then
  begin
    // Decode video frame
    avcodec_decode_video2(dec_ctxVid, VidFrame, frameFinished, @VidPacket);

    // Did we get a video frame?
    if frameFinished>0 then
    begin
        // Convert the image from its native format to RGB
      sws_scale
      (
        ImgConvContext,
        @VidFrame.data,
        @VidFrame.linesize,
        0,
        dec_ctxVid.height,
        @pict.data,
        @pict.linesize
      );

        // Save the frame to disk
      inc(i);

          for i := 0 to bmp.Height - 1 do
  CopyMemory ( bmp.ScanLine[i], pointer(integer(pict.data[0]) + bmp.Width * 4 * i), bmp.Width * 4);

     StretchBlt(Form1.PaintBox1.Canvas.Handle,0,0,PaintBox1.Width,PaintBox1.Height,bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,SRCCOPY)
     // Form1.PaintBox1.Canvas.Draw(0,0,bmp);
    end;
    // Free the packet that was allocated by av_read_frame
    av_free_packet(@VidPacket);
  end;
end;

// Free the RGB image
av_free(buffer);
av_free(pict);
    bmp.free;
// Free the YUV frame
av_free(VidFrame);

// Close the codec
avcodec_close(dec_ctxVid);

// Close the video file
avformat_close_input(fmt_ctx);

except
 on E: Exception do
  Memo1.Lines.add(E.ClassName+ ': '+ E.Message);
 end;

Может кому-то и пригодится

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.