<div dir="ltr"><div dir="ltr"><div dir="ltr">Hey Strahinja!<div><br></div><div>Thank you very much for taking the time to help me out.</div><div><br></div><div>the pgm snippet you sent looks the same as the decode video example I am seeing.</div><div><br></div><div>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).</div><div><br></div><div><div>/*</div><div>* Copyright (c) 2001 Fabrice Bellard</div><div>*</div><div>* Permission is hereby granted, free of charge, to any person obtaining a copy</div><div>* of this software and associated documentation files (the "Software"), to deal</div><div>* in the Software without restriction, including without limitation the rights</div><div>* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</div><div>* copies of the Software, and to permit persons to whom the Software is</div><div>* furnished to do so, subject to the following conditions:</div><div>*</div><div>* The above copyright notice and this permission notice shall be included in</div><div>* all copies or substantial portions of the Software.</div><div>*</div><div>* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</div><div>* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</div><div>* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</div><div>* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</div><div>* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</div><div>* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</div><div>* THE SOFTWARE.</div><div>*/</div><div><br></div><div>/**</div><div>* @file</div><div>* video decoding with libavcodec API example</div><div>*</div><div>* @example decode_video.c</div><div>*/</div><div><br></div><div>#include <stdio.h></div><div>#include <stdlib.h></div><div>#include <string.h></div><div><br></div><div>extern "C"</div><div>{</div><div>#include <libavcodec/avcodec.h></div><div>#include <libavformat\avformat.h></div><div>}</div><div><br></div><div>#define INBUF_SIZE 4096</div><div><br></div><div>static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,</div><div><span style="white-space:pre">     </span>char *filename)</div><div>{</div><div><span style="white-space:pre">       </span>FILE *f;</div><div><span style="white-space:pre">      </span>int i;</div><div><br></div><div><span style="white-space:pre">       </span>f = fopen(filename, "w");</div><div><span style="white-space:pre">   </span>fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);</div><div><span style="white-space:pre">   </span>for (i = 0; i < ysize; i++)</div><div><span style="white-space:pre">                </span>fwrite(buf + i * wrap, 1, xsize, f);</div><div><span style="white-space:pre">  </span>fclose(f);</div><div>}</div><div><br></div><div>static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,</div><div><span style="white-space:pre">  </span>const char *filename)</div><div>{</div><div><span style="white-space:pre"> </span>char buf[1024];</div><div><span style="white-space:pre">       </span>int ret;</div><div><br></div><div><span style="white-space:pre">     </span>ret = avcodec_send_packet(dec_ctx, pkt);</div><div><span style="white-space:pre">      </span>if (ret < 0) {</div><div><span style="white-space:pre">             </span>fprintf(stderr, "Error sending a packet for decoding\n");</div><div><span style="white-space:pre">           </span>exit(1);</div><div><span style="white-space:pre">      </span>}</div><div><br></div><div><span style="white-space:pre">    </span>while (ret >= 0) {</div><div><span style="white-space:pre">         </span>ret = avcodec_receive_frame(dec_ctx, frame);</div><div><span style="white-space:pre">          </span>if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)</div><div><span style="white-space:pre">                     </span>return;</div><div><span style="white-space:pre">               </span>else if (ret < 0) {</div><div><span style="white-space:pre">                        </span>fprintf(stderr, "Error during decoding\n");</div><div><span style="white-space:pre">                 </span>exit(1);</div><div><span style="white-space:pre">              </span>}</div><div><br></div><div><span style="white-space:pre">            </span>printf("saving frame %3d\n", dec_ctx->frame_number);</div><div><span style="white-space:pre">             </span>fflush(stdout);</div><div><br></div><div><span style="white-space:pre">              </span>/* the picture is allocated by the decoder. no need to</div><div><span style="white-space:pre">                </span>free it */</div><div><span style="white-space:pre">            </span>_snprintf(buf, sizeof(buf), "%s-%d.pgm", filename, dec_ctx->frame_number);</div><div><span style="white-space:pre">               </span>pgm_save(frame->data[0], frame->linesize[0],</div><div><span style="white-space:pre">                    </span>frame->width, frame->height, buf);</div><div><span style="white-space:pre">      </span>}</div><div>}</div><div><br></div><div>int main(int argc, char **argv)</div><div>{</div><div><span style="white-space:pre">      </span>const char *filename, *outfilename;</div><div><span style="white-space:pre">   </span>const AVCodec *codec;</div><div><span style="white-space:pre"> </span>AVCodecParserContext *parser;</div><div><span style="white-space:pre"> </span>AVCodecContext *c = NULL;</div><div><span style="white-space:pre">     </span>FILE *f;</div><div><span style="white-space:pre">      </span>AVFrame *frame;</div><div><span style="white-space:pre">       </span>uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];</div><div><span style="white-space:pre">     </span>uint8_t *data;</div><div><span style="white-space:pre">        </span>size_t   data_size;</div><div><span style="white-space:pre"> </span>int ret;</div><div><span style="white-space:pre">      </span>AVPacket *pkt;</div><div><br></div><div><span style="white-space:pre">       </span>if (argc <= 2) {</div><div><span style="white-space:pre">           </span>fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);</div><div><span style="white-space:pre">             </span>exit(0);</div><div><span style="white-space:pre">      </span>}</div><div><span style="white-space:pre">     </span>filename = argv[1];</div><div><span style="white-space:pre">   </span>outfilename = argv[2];</div><div><br></div><div><span style="white-space:pre">       </span>pkt = av_packet_alloc();</div><div><span style="white-space:pre">      </span>if (!pkt)</div><div><span style="white-space:pre">             </span>exit(1);</div><div><br></div><div><span style="white-space:pre">     </span>/* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */</div><div><span style="white-space:pre">      </span>memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);</div><div><br></div><div><br></div><div><span style="white-space:pre">        </span>//---from test1.cpp</div><div><span style="white-space:pre">   </span>AVFormatContext *pFormatContext = avformat_alloc_context();</div><div><span style="white-space:pre">   </span>if (!pFormatContext) {</div><div><span style="white-space:pre">                </span>fprintf(stderr, "ERROR could not allocate memory for Format Context");</div><div><span style="white-space:pre">              </span>return -1;</div><div><span style="white-space:pre">    </span>}</div><div><span style="white-space:pre">     </span>if (avformat_open_input(&pFormatContext, argv[1], NULL, NULL) != 0) {</div><div><span style="white-space:pre">             </span>fprintf(stderr, "ERROR could not open the file");</div><div><span style="white-space:pre">           </span>return -1;</div><div><span style="white-space:pre">    </span>}</div><div><span style="white-space:pre">     </span>AVCodecParameters *pLocalCodecParameters = NULL;</div><div><span style="white-space:pre">      </span>pLocalCodecParameters = pFormatContext->streams[0]->codecpar;</div><div><span style="white-space:pre">   </span>//-----</div><div><br></div><div><span style="white-space:pre">      </span>/* find the video decoder */</div><div><span style="white-space:pre">  </span>//codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);</div><div><span style="white-space:pre">    </span>codec = avcodec_find_decoder(pLocalCodecParameters->codec_id);</div><div><span style="white-space:pre">     </span>if (!codec) {</div><div><span style="white-space:pre">         </span>fprintf(stderr, "Codec not found\n");</div><div><span style="white-space:pre">               </span>exit(1);</div><div><span style="white-space:pre">      </span>}</div><div><br></div><div><span style="white-space:pre">    </span>parser = av_parser_init(codec->id);</div><div><span style="white-space:pre">        </span>if (!parser) {</div><div><span style="white-space:pre">                </span>fprintf(stderr, "parser not found\n");</div><div><span style="white-space:pre">              </span>exit(1);</div><div><span style="white-space:pre">      </span>}</div><div><br></div><div><span style="white-space:pre">    </span>c = avcodec_alloc_context3(codec);</div><div><span style="white-space:pre">    </span>if (!c) {</div><div><span style="white-space:pre">             </span>fprintf(stderr, "Could not allocate video codec context\n");</div><div><span style="white-space:pre">                </span>exit(1);</div><div><span style="white-space:pre">      </span>}</div><div><br></div><div><span style="white-space:pre">    </span>/* For some codecs, such as msmpeg4 and mpeg4, width and height</div><div><span style="white-space:pre">       </span>MUST be initialized there because this information is not</div><div><span style="white-space:pre">     </span>available in the bitstream. */</div><div><br></div><div><span style="white-space:pre">       </span>/* open it */</div><div><span style="white-space:pre"> </span>if (avcodec_open2(c, codec, NULL) < 0) {</div><div><span style="white-space:pre">           </span>fprintf(stderr, "Could not open codec\n");</div><div><span style="white-space:pre">          </span>exit(1);</div><div><span style="white-space:pre">      </span>}</div><div><br></div><div><span style="white-space:pre">    </span>f = fopen(filename, "rb");</div><div><span style="white-space:pre">  </span>if (!f) {</div><div><span style="white-space:pre">             </span>fprintf(stderr, "Could not open %s\n", filename);</div><div><span style="white-space:pre">           </span>exit(1);</div><div><span style="white-space:pre">      </span>}</div><div><br></div><div><span style="white-space:pre">    </span>frame = av_frame_alloc();</div><div><span style="white-space:pre">     </span>if (!frame) {</div><div><span style="white-space:pre">         </span>fprintf(stderr, "Could not allocate video frame\n");</div><div><span style="white-space:pre">                </span>exit(1);</div><div><span style="white-space:pre">      </span>}</div><div><br></div><div><span style="white-space:pre">    </span>while (!feof(f)) {</div><div><span style="white-space:pre">            </span>/* read raw data from the input file */</div><div><span style="white-space:pre">               </span>data_size = fread(inbuf, 1, INBUF_SIZE, f);</div><div><span style="white-space:pre">           </span>if (!data_size)</div><div><span style="white-space:pre">                       </span>break;</div><div><br></div><div><span style="white-space:pre">               </span>/* use the parser to split the data into frames */</div><div><span style="white-space:pre">            </span>data = inbuf;</div><div><span style="white-space:pre">         </span>while (data_size > 0) {</div><div><span style="white-space:pre">                    </span>ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,</div><div><span style="white-space:pre">                               </span>data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);</div><div><span style="white-space:pre">                  </span>if (ret < 0) {</div><div><span style="white-space:pre">                             </span>fprintf(stderr, "Error while parsing\n");</div><div><span style="white-space:pre">                           </span>exit(1);</div><div><span style="white-space:pre">                      </span>}</div><div><span style="white-space:pre">                     </span>data += ret;</div><div><span style="white-space:pre">                  </span>data_size -= ret;</div><div><br></div><div><span style="white-space:pre">                    </span>if (pkt->size)</div><div><span style="white-space:pre">                             </span>decode(c, frame, pkt, outfilename);</div><div><span style="white-space:pre">           </span>}</div><div><span style="white-space:pre">     </span>}</div><div><br></div><div><span style="white-space:pre">    </span>/* flush the decoder */</div><div><span style="white-space:pre">       </span>decode(c, frame, NULL, outfilename);</div><div><br></div><div><span style="white-space:pre"> </span>fclose(f);</div><div><br></div><div><span style="white-space:pre">   </span>av_parser_close(parser);</div><div><span style="white-space:pre">      </span>avcodec_free_context(&c);</div><div><span style="white-space:pre"> </span>av_frame_free(&frame);</div><div><span style="white-space:pre">    </span>av_packet_free(&pkt);</div><div><br></div><div><span style="white-space:pre">    </span>return 0;</div><div>}</div></div><div><br></div><div>Please let me know if you see anything incorrect with this.</div><div><br></div><div>Thanks!</div><div><br></div></div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Chris Bennett</div><div>Lead Animation Pipeline TD - Television</div><img src="http://www.dreamworksanimation.com/brand/signatures/Central/CentralEmailSigs06.jpg"><br></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Fri, Aug 31, 2018 at 10:48 AM, Strahinja Radman <span dir="ltr"><<a href="mailto:dr.strashni@gmail.com" target="_blank">dr.strashni@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-GB" link="blue" vlink="#954F72"><div class="m_-8290187395701461958WordSection1"><p class="MsoNormal">Hi Chris,</p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">To me that looks like you wrote some wrong data inside the picture and it looks skewed. Some indexes could be wrong.</p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, const char *filename)</p><p class="MsoNormal">{</p><p class="MsoNormal">                FILE *f;</p><p class="MsoNormal">                int i;</p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">                f = fopen(filename, "w");</p><p class="MsoNormal">                fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);</p><p class="MsoNormal">                for (i = 0; i < ysize; i++)</p><p class="MsoNormal">                              <wbr>  fwrite(buf + i * wrap, 1, xsize, f);</p><p class="MsoNormal">                fclose(f);</p><p class="MsoNormal">}</p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">The above method should be called with pgm_save(frame->data[0], frame->linesize[0], frame->width, frame->height, “name.pgm”);</p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">Kind regards,</p><p class="MsoNormal">Strahinja</p><p class="MsoNormal"><u></u> <u></u></p><div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm"><p class="MsoNormal" style="border:none;padding:0cm"><b>From: </b><a href="mailto:chris.bennett@dreamworks.com" target="_blank">Chris Bennett</a><br><b>Sent: </b>31 August 2018 19:41<br><b>To: </b><a href="mailto:libav-user@ffmpeg.org" target="_blank">libav-user@ffmpeg.org</a><br><b>Subject: </b>[Libav-user] decode_video example pgm files distorted? (v4.0.2 Winbuild)</p></div><div><div class="h5"><p class="MsoNormal"><u></u> <u></u></p><div><div><div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">Hello All!<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">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.<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">I cloned 4.0.2 and compiled the libraries (MS VS 2013 x64) without issue.<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">I decided to try the examples as a learning opportunity, choosing decode_video. I ended up with this result:  </span><span style="font-size:12.0pt"><a href="https://drive.google.com/file/d/1DPu1EaM7i5J3LFaaGmZNO4Hoi_QzVcKM/view?usp=sharing" target="_blank">https://drive.google.com/file/<wbr>d/1DPu1EaM7i5J3LFaaGmZNO4Hoi_<wbr>QzVcKM/view?usp=sharing</a></span><span style="font-size:9.5pt"> <u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">At the top and bottom of the image there is is wavy distortion. I am not sure what is causing it.<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">The source file is a quicktime of a Motion JPEG video.<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">I had also compiled the exe as well and ran this command<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">ffmpeg.exe -ss 0.5 -i my_quicktime.mov  -t 1 -s 480x300 -f image2 C:/frame-%03d.jpg<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">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)<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">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.<u></u><u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">Thanks,<u></u><u></u></span></p></div></div></div></div><p class="MsoNormal" style="background:white"><span style="font-size:9.5pt">Chris Bennett<u></u><u></u></span></p><p class="MsoNormal"><u></u> <u></u></p></div></div></div></div><br>______________________________<wbr>_________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/<wbr>listinfo/libav-user</a><br>
<br></blockquote></div><br></div>