Home > other >  Consult FFmpeg great god: I have the latest version of decoding the h264 files, after decoding YUV42
Consult FFmpeg great god: I have the latest version of decoding the h264 files, after decoding YUV42

Time:11-26

Consult FFmpeg great god: I have the latest version of decoding the h264 files, after decoding YUV420 files, color mismatch, is this why???????
# pragma once
# include & lt; Iostream>
Extern "C"
{
# include & lt; Libavcodec/avcodec. H>
# include & lt; Libavutil/frame. H>
# include & lt; Libavformat/avformat. H>
# include & lt; Libswscale/swscale. H>
# include & lt; Libavutil/imgutils. H>
};

# define INBUF_SIZE 4096

using namespace std;

Void pgm_save (unsigned char * buf, int wrap, int xsize, int ysize, char * filename)
{
The FILE * f;
int i;

F=fopen (filename, ab + "");
Fprintf (f, "P5 \ n \ n \ n % d % d % d", xsize, ysize, 255);
For (I=0; i Fwrite (buf + I * wrap, 1, xsize, f);
The fclose (f);
}

Void decode (AVCodecContext * dec_ctx, AVFrame * frame, AVPacket * PKT, const char * filename)
{
Char buf [1024].
Int ret.

Ret=avcodec_send_packet (dec_ctx PKT);
If (ret & lt; 0)
{
Fprintf (stderr, "Error sending a packet for decoding \ n");
exit(1);
}

While (ret & gt;=0)
{

Ret=avcodec_receive_frame (dec_ctx, frame);
If (ret==AVERROR (EAGAIN) | | ret==AVERROR_EOF)
return;
Else if (ret & lt; 0)
{
Fprintf (stderr, "Error during decoding \ n");
exit(1);
}

//sws_scale (imgCtx, frame - & gt; The data frame - & gt; Linesize, 0, dec_ctx - & gt; Height, yuv - & gt; Data, yuv - & gt; Linesize);

Printf (" saving frame % 3 d \ n ", dec_ctx - & gt; Frame_number);
The fflush (stdout);

/* the picture is allocated by the decoder. No need to free it */
Snprintf (buf, sizeof (buf), "% s, % d", filename, dec_ctx - & gt; Frame_number);
Pgm_save (frame - & gt; Data [0], frame - & gt; Linesize [0], dec_ctx - & gt; Width, dec_ctx - & gt; Height, buf);//Y
Pgm_save (frame - & gt; Data [1], the frame - & gt; Linesize [1], dec_ctx - & gt; Width/2, dec_ctx - & gt; Height/2, buf);//U
Pgm_save (frame - & gt; Data [2], the frame - & gt; Linesize [2], dec_ctx - & gt; Width/2, dec_ctx - & gt; Height/2, buf);//V
}
}

Int main (int arg c, char * * argv)
{
Const char * filename="test3. H264", * outfilename="frames \ \ frame";
Const AVCodec * codec;
AVCodecParserContext * parser;
AVCodecContext * c=NULL;
The FILE * f;
AVFrame * frame;
Uint8_t inbuf [INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
Uint8_t * data;
Size_t data_size.
Int ret.
AVPacket * PKT.

PKT=av_packet_alloc ();
if (! PKT)
exit(1);

/* set end of buffer to 0 (this ensures that no overreading happens for on MPEG streams) */
Memset (inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);

/* find the mpeg-1 video decoder */
//codec=avcodec_find_decoder (AV_CODEC_ID_MPEG1VIDEO);
The codec=avcodec_find_decoder (AV_CODEC_ID_H264);
if (! The codec) {
Fprintf (stderr, "Codec not found \ n");
exit(1);
}

The parser=av_parser_init (codec - & gt; Id);
if (! The parser) {
Fprintf (stderr, "parser not found \ n");
exit(1);
}

C=avcodec_alloc_context3 (codec);
if (! C) {
Fprintf (stderr, "Could not the allocate video codec context \ n");
exit(1);
}

/* */open it
If (avcodec_open2 (c, codec, NULL) & lt; 0 {
Fprintf (stderr, "Could not open the codec \ n");
exit(1);
}

F=fopen (filename, "rb");
if (! F) {
Fprintf (stderr, "Could not open % s \ n", filename);
exit(1);
}

Frame=av_frame_alloc ();
if (! Frame) {
Fprintf (stderr, "Could not the allocate video frame \ n");
exit(1);
}

while (! The feof (f)) {
/* read raw data from the input file */
Data_size=fread (inbuf, 1, INBUF_SIZE, f);
if (! Data_size)
break;

/* use the parser to split the data into frames */
Data=https://bbs.csdn.net/topics/inbuf;
While (data_size & gt; 0 {
Ret=av_parser_parse2 (parser, c, & amp; PKT - & gt; The data, & amp; PKT - & gt; The size of the data, data_size AV_NOPTS_VALUE AV_NOPTS_VALUE, 0);
If (ret & lt; 0 {
Fprintf (stderr, "Error while parsing \ n");
exit(1);
}
The data +=ret;
Data_size -=ret;

If (PKT - & gt; The size)
Decode (c, frame, PKT, outfilename);
}
}

/* flush the decoder */
Decode (c, frame, NULL, outfilename);

The fclose (f);

Av_parser_close (parser);
Avcodec_free_context (& amp; c);
Av_frame_free (& amp; Frame);
Av_packet_free (& amp; PKT);
system("pause");
return 0;
}
FFmpeg4.0.2 after decoding the YUV file:
Low version decoding normal YUV file:


CodePudding user response:

Format must be turned in the wrong, save to file, using tools and have a look

CodePudding user response:

Decoding the YUV generated should be no problem, Y data read correctly, is the UV is not correct,
You send display YUV code to determine what is the problem,
Intuition is:
One may be behind each row Panding fill empty part of the value is different,
The second is maybe you accidentally Y data line width of the line [0] used in UV, so,nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related