0

What AvPixelFormat i should use for *.png (PNG-24 and PNG-8) image? I am trying to convert from png to PIX_FMT_YUV420P using sws_scale

Edit, code:

avpicture_fill((AVPicture*)m_encode_inpic, (uint8_t*)imagePixels, <WHAT PNG PIXELS FORMAT?>, m_imgInputWidth, m_imgInputHeight);                   //imagePixels is png bytes 
avpicture_fill((AVPicture*)m_encode_outpic, m_encode_outbuffer, PIX_FMT_YUV420P, m_encode_codec_context->width, m_encode_codec_context->height);

av_image_alloc(m_encode_outpic->data, m_encode_outpic->linesize, m_encode_codec_context->width, m_encode_codec_context->height, m_encode_codec_context->pix_fmt, 1);

struct SwsContext* fooContext = sws_getContext(m_videoOutputWidth, m_videoOutputHeight, PIX_FMT_RGB32, m_encode_codec_context->width, m_encode_codec_context->height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(fooContext, m_encode_inpic->data, m_encode_inpic->linesize, 0, m_encode_codec_context->height, m_encode_outpic->data, m_encode_outpic->linesize);   

Jpeg pixel format is AV_PIX_FMT_YUVJ420P, but what format is for PNG?

Rhot
  • 63
  • 2
  • 9

1 Answers1

3

You must decode the PNG first. SWS only works on raw pixels. Think of a compressed graphics file kind of like a zip file. You must first decompress it before you can see what is inside. Usually your browser, or other program does this for you automatically. You can use libavcodec to decompress the PNG then use pix_fmt form the codec context.

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • Look at my first post. I add sample code. I don't know how convert differently. And I don't want to use extra libs such as libpng. – Rhot Mar 22 '14 at 23:10
  • Your file is currently compressed. It must be decompresses first. Period. I can not say it any differently. sws_scale can not decode PNG, or anything for that matter. It can only convert one pixel format to another. – szatmary Mar 23 '14 at 01:46
  • yuv420p is incompatible with the PNG encoder. An alternative is rgb24 as pixel format – Agile Bean Jul 28 '18 at 15:06
  • Any example for save png from AVPacket? I saved it as jpeg but image look so bad. – Trương Quốc Khánh Jan 25 '21 at 10:45