[Libav-user] decode_video example pgm files distorted?(v4.0.2Winbuild)

Chris Bennett chris.bennett at dreamworks.com
Tue Sep 4 23:42:20 EEST 2018


Hey Strahinja,

Yeah that is what I have. Here is the link to the latest version of the
modified code:

https://drive.google.com/file/d/1UQaGv4EwEj81hqk7GNeVz5hGFG0784RD/view?usp=sharing

Let me know what you think.

Thanks

Chris Bennett

On Tue, Sep 4, 2018 at 12:06 PM, Strahinja Radman <dr.strashni at gmail.com>
wrote:

> Hey Chris,
>
>
>
> I am not sure how exactly you modified your code but this should be the
> look of the loop
>
>
>
> while(av_read_frame(fmt_ctx, &packet) > 0)
>
> {
>
>                 decode(c, frame, pkt, outfilename);
>
> }
>
>
>
> Kind regards,
>
> Strahinja
>
>
>
> *From: *Chris Bennett <chris.bennett at dreamworks.com>
> *Sent: *04 September 2018 20:54
>
> *To: *This list is about using libavcodec, libavformat, libavutil,
> libavdevice and libavfilter. <libav-user at ffmpeg.org>
> *Subject: *Re: [Libav-user] decode_video example pgm files
> distorted?(v4.0.2Winbuild)
>
>
>
> Hm. Here is something strange.
>
>
>
> When I try using  while(av_read_frame(fmt_ctx, &packet) > 0), the method
> immediately passes, making nothing happen.
>
>
>
> I turned on debug in my stream, and this is the output (not much makes
> sense/looks useful to me)
>
>
>
> https://drive.google.com/file/d/1Twft75sGLGGsHN5tuwkcI1KQVsPM5
> lt4/view?usp=sharing
>
>
>
> Let me know what you think. Thanks!
>
>
> Chris Bennett
>
>
>
> On Sat, Sep 1, 2018 at 12:48 AM, Strahinja Radman <dr.strashni at gmail.com>
> wrote:
>
> Hey Chris,
>
>
>
> Can you try using
>
>                 while(av_read_frame(fmt_ctx, &packet) > 0)
>
> instead of using
>
>                 while(!feof(f))
>
>
>
> and then just send a packet to your decode method instead of a raw frame?
>
>
>
> *From: *Chris Bennett <chris.bennett at dreamworks.com>
> *Sent: *31 August 2018 22:57
> *To: *This list is about using libavcodec, libavformat, libavutil,
> libavdevice and libavfilter. <libav-user at ffmpeg.org>
> *Subject: *Re: [Libav-user] decode_video example pgm files distorted?
> (v4.0.2Winbuild)
>
>
>
> Hey Strahinja!
>
>
>
> Thank you very much for taking the time to help me out.
>
>
>
> the pgm snippet you sent looks the same as the decode video example I am
> seeing.
>
>
>
> Here is the code that I am compiling and running if it helps, hopefully
> its not too long. ( I had to add an extern "C" wrapper since my goal is to
> build c++ tools with this, and also changed how the example finds the
> decoder).
>
>
>
> /*
>
> * Copyright (c) 2001 Fabrice Bellard
>
> *
>
> * Permission is hereby granted, free of charge, to any person obtaining a
> copy
>
> * of this software and associated documentation files (the "Software"), to
> deal
>
> * in the Software without restriction, including without limitation the
> rights
>
> * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
>
> * copies of the Software, and to permit persons to whom the Software is
>
> * furnished to do so, subject to the following conditions:
>
> *
>
> * The above copyright notice and this permission notice shall be included
> in
>
> * all copies or substantial portions of the Software.
>
> *
>
> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
> OR
>
> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
>
> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
>
> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
> OTHER
>
> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> FROM,
>
> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
>
> * THE SOFTWARE.
>
> */
>
>
>
> /**
>
> * @file
>
> * video decoding with libavcodec API example
>
> *
>
> * @example decode_video.c
>
> */
>
>
>
> #include <stdio.h>
>
> #include <stdlib.h>
>
> #include <string.h>
>
>
>
> extern "C"
>
> {
>
> #include <libavcodec/avcodec.h>
>
> #include <libavformat\avformat.h>
>
> }
>
>
>
> #define INBUF_SIZE 4096
>
>
>
> static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
>
>                 char *filename)
>
> {
>
>                 FILE *f;
>
>                 int i;
>
>
>
>                 f = fopen(filename, "w");
>
>                 fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
>
>                 for (i = 0; i < ysize; i++)
>
>                                 fwrite(buf + i * wrap, 1, xsize, f);
>
>                 fclose(f);
>
> }
>
>
>
> static 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 < 0) {
>
>                                 fprintf(stderr, "Error sending a packet
> for decoding\n");
>
>                                 exit(1);
>
>                 }
>
>
>
>                 while (ret >= 0) {
>
>                                 ret = avcodec_receive_frame(dec_ctx,
> frame);
>
>                                 if (ret == AVERROR(EAGAIN) || ret ==
> AVERROR_EOF)
>
>                                                 return;
>
>                                 else if (ret < 0) {
>
>                                                 fprintf(stderr, "Error
> during decoding\n");
>
>                                                 exit(1);
>
>                                 }
>
>
>
>                                 printf("saving frame %3d\n",
> dec_ctx->frame_number);
>
>                                 fflush(stdout);
>
>
>
>                                 /* the picture is allocated by the
> decoder. no need to
>
>                                 free it */
>
>                                 _snprintf(buf, sizeof(buf), "%s-%d.pgm",
> filename, dec_ctx->frame_number);
>
>                                 pgm_save(frame->data[0],
> frame->linesize[0],
>
>                                                 frame->width,
> frame->height, buf);
>
>                 }
>
> }
>
>
>
> int main(int argc, char **argv)
>
> {
>
>                 const char *filename, *outfilename;
>
>                 const AVCodec *codec;
>
>                 AVCodecParserContext *parser;
>
>                 AVCodecContext *c = NULL;
>
>                 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;
>
>
>
>                 if (argc <= 2) {
>
>                                 fprintf(stderr, "Usage: %s <input file>
> <output file>\n", argv[0]);
>
>                                 exit(0);
>
>                 }
>
>                 filename = argv[1];
>
>                 outfilename = argv[2];
>
>
>
>                 pkt = av_packet_alloc();
>
>                 if (!pkt)
>
>                                 exit(1);
>
>
>
>                 /* set end of buffer to 0 (this ensures that no
> overreading happens for damaged MPEG streams) */
>
>                 memset(inbuf + INBUF_SIZE, 0,
> AV_INPUT_BUFFER_PADDING_SIZE);
>
>
>
>
>
>                 //---from test1.cpp
>
>                 AVFormatContext *pFormatContext = avformat_alloc_context();
>
>                 if (!pFormatContext) {
>
>                                 fprintf(stderr, "ERROR could not allocate
> memory for Format Context");
>
>                                 return -1;
>
>                 }
>
>                 if (avformat_open_input(&pFormatContext, argv[1], NULL,
> NULL) != 0) {
>
>                                 fprintf(stderr, "ERROR could not open the
> file");
>
>                                 return -1;
>
>                 }
>
>                 AVCodecParameters *pLocalCodecParameters = NULL;
>
>                 pLocalCodecParameters = pFormatContext->streams[0]->
> codecpar;
>
>                 //-----
>
>
>
>                 /* find the video decoder */
>
>                 //codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
>
>                 codec = avcodec_find_decoder(pLocalCodecParameters->codec_
> id);
>
>                 if (!codec) {
>
>                                 fprintf(stderr, "Codec not found\n");
>
>                                 exit(1);
>
>                 }
>
>
>
>                 parser = av_parser_init(codec->id);
>
>                 if (!parser) {
>
>                                 fprintf(stderr, "parser not found\n");
>
>                                 exit(1);
>
>                 }
>
>
>
>                 c = avcodec_alloc_context3(codec);
>
>                 if (!c) {
>
>                                 fprintf(stderr, "Could not allocate video
> codec context\n");
>
>                                 exit(1);
>
>                 }
>
>
>
>                 /* For some codecs, such as msmpeg4 and mpeg4, width and
> height
>
>                 MUST be initialized there because this information is not
>
>                 available in the bitstream. */
>
>
>
>                 /* open it */
>
>                 if (avcodec_open2(c, codec, NULL) < 0) {
>
>                                 fprintf(stderr, "Could not open 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 allocate video
> frame\n");
>
>                                 exit(1);
>
>                 }
>
>
>
>                 while (!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 = inbuf;
>
>                                 while (data_size > 0) {
>
>                                                 ret =
> av_parser_parse2(parser, c, &pkt->data, &pkt->size,
>
>                                                                 data,
> data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
>
>                                                 if (ret < 0) {
>
>
> fprintf(stderr, "Error while parsing\n");
>
>                                                                 exit(1);
>
>                                                 }
>
>                                                 data += ret;
>
>                                                 data_size -= ret;
>
>
>
>                                                 if (pkt->size)
>
>                                                                 decode(c,
> frame, pkt, outfilename);
>
>                                 }
>
>                 }
>
>
>
>                 /* flush the decoder */
>
>                 decode(c, frame, NULL, outfilename);
>
>
>
>                 fclose(f);
>
>
>
>                 av_parser_close(parser);
>
>                 avcodec_free_context(&c);
>
>                 av_frame_free(&frame);
>
>                 av_packet_free(&pkt);
>
>
>
>                 return 0;
>
> }
>
>
>
> Please let me know if you see anything incorrect with this.
>
>
>
> Thanks!
>
>
>
>
> Chris Bennett
>
> Lead Animation Pipeline TD - Television
>
> [image:
> http://www.dreamworksanimation.com/brand/signatures/Central/CentralEmailSigs06.jpg]
>
>
>
> On Fri, Aug 31, 2018 at 10:48 AM, Strahinja Radman <dr.strashni at gmail.com>
> wrote:
>
> Hi Chris,
>
>
>
> To me that looks like you wrote some wrong data inside the picture and it
> looks skewed. Some indexes could be wrong.
>
>
>
> void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, const
> char *filename)
>
> {
>
>                 FILE *f;
>
>                 int i;
>
>
>
>                 f = fopen(filename, "w");
>
>                 fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
>
>                 for (i = 0; i < ysize; i++)
>
>                                 fwrite(buf + i * wrap, 1, xsize, f);
>
>                 fclose(f);
>
> }
>
>
>
> The above method should be called with pgm_save(frame->data[0],
> frame->linesize[0], frame->width, frame->height, “name.pgm”);
>
>
>
> Kind regards,
>
> Strahinja
>
>
>
> *From: *Chris Bennett <chris.bennett at dreamworks.com>
> *Sent: *31 August 2018 19:41
> *To: *libav-user at ffmpeg.org
> *Subject: *[Libav-user] decode_video example pgm files distorted? (v4.0.2
> Winbuild)
>
>
>
> Hello All!
>
>
>
> My apologies if this is a rookie question as I am just now learning the
> innards of ffmpeg, but I am reaching out hoping someone may have had the
> same issue as myself or has any advice.
>
>
>
> I cloned 4.0.2 and compiled the libraries (MS VS 2013 x64) without issue.
>
> I decided to try the examples as a learning opportunity, choosing
> decode_video. I ended up with this result:  https://drive.google.com/file/
> d/1DPu1EaM7i5J3LFaaGmZNO4Hoi_QzVcKM/view?usp=sharing
>
>
>
> At the top and bottom of the image there is is wavy distortion. I am not
> sure what is causing it.
>
> The source file is a quicktime of a Motion JPEG video.
>
>
>
> I had also compiled the exe as well and ran this command
>
>
>
> ffmpeg.exe -ss 0.5 -i my_quicktime.mov  -t 1 -s 480x300 -f image2
> C:/frame-%03d.jpg
>
>
>
> and those images turned out just fine (I am sure this probably runs some
> completely different underlying code but I just wanted to make sure what I
> compiled was capable of a successful result)
>
>
>
> I figure I am doing something incorrectly but I do not know where else to
> look. If anyone has any advice on what to tinker with, I would greatly
> appreciate it.
>
>
>
> Thanks,
>
> Chris Bennett
>
>
>
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
>
>
>
>
>
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
>
>
>
>
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20180904/85138922/attachment.html>


More information about the Libav-user mailing list