<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif;" dir="ltr">
<p>Hi,</p>
<p><br>
</p>
<p>I'm trying to decode a Raw Stream using the ffmpeg API (version 2.8) which is sent over TCP with the following gstreamer pipeline on the server side:</p>
<p></p>
<div> > nv4l2src name=source ! video/x-raw-yuv,format=(fourcc)I420,width=1552,height=720 ! tcpserversink name=sink port=50000</div>
<div>In this case TCP is necessary because artifacts are not acceptable in this kind of application.</div>
<div><br>
</div>
<div><span style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">When reading the first frame it crashes in the <span style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">avcodec_decode_video2
line </span>with an </span><span style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">unhandled exception caught 0xC0000005 error.</span><br>
</div>
<div>My program works for other types of streams e.g. H264, only decoding raw streams seems to be the problem. </div>
<div><br>
</div>
<div><span style="font-size: 12pt;">When I play the stream with ffplay it works just fine. I also tried to put the packet buffer directly into an
</span>AVFrame which works, but the packet size changes and the images get distorted over time. </div>
<div><span style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">Due to restriction i'm not able to provide an examplatory stream.</span><br>
</div>
<div><span style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;"><br>
</span></div>
<div>How can I decode the raw stream appropriately using <span style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">
avcodec_decode_video2?</span></div>
<div>What am I doing wrong?</div>
<div><br>
</div>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">
Used Libraries:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">
<div><span style="font-size: 12pt;">- avcodec-57.dll</span><br>
</div>
<div>- avformat-57.dll</div>
<div>- avutil-55.dll</div>
<div>- swresample-2.dll</div>
<div>- swscale-4.dll</div>
<div><br>
</div>
</div>
</div>
<div>best regards,</div>
<div>Tobi</div>
<div><br>
</div>
<div><span style="font-size: 12pt;">Attached is the code and logs:</span></div>
<div><span style="font-size: 12pt;"><br>
</span></div>
<div><span style="font-size: 12pt;">The ffmpeg </span><span style="font-size: 12pt;">initialization is as follows:</span><br>
</div>
<div>
<div>
<div> _avFormatContext = avformat_alloc_context();</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_register_all();</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>avcodec_register_all();</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>avformat_network_init();</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>packet.data = NULL;</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>_avFormatContext->iformat = av_find_input_format("rawvideo");</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>AVDictionary *options = NULL;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>std::stringstream ss;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>ss << _width << "x" << _height;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_dict_set(&options, "video_size", ss.str().c_str(), 0);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_dict_set(&options, "pixel_format", "yuv420p", 0);<span class="Apple-tab-span" style="white-space:pre">
</span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if( avformat_open_input(&_avFormatContext, _fileToPlay.c_str(), NULL, &options) < 0)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return false;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_dict_set(&options, "video_size", ss.str().c_str(), 0);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_dict_set(&options, "pixel_format", "yuv420p", 0);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if(avformat_find_stream_info(_avFormatContext, &options) < 0)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return false;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>for(size_t i =0; i<_avFormatContext->nb_streams;i++)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if(_avFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>video_stream_index = i;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if( video_stream_index == -1)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return false;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_init_packet(&packet);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>oc = avformat_alloc_context();</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_read_play(_avFormatContext);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream = _avFormatContext->streams[video_stream_index];</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>codec = avcodec_find_encoder_by_name("rawvideo");</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if (!codec)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return false;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>_avCodecContext = avcodec_alloc_context3(codec);<span class="Apple-tab-span" style="white-space:pre">
</span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if( avcodec_copy_context(_avCodecContext, stream->codec) != 0)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return false;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>_avCodecContext->thread_count = 1;</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if (avcodec_open2(_avCodecContext, codec, NULL) < 0)
<span class="Apple-tab-span" style="white-space:pre"></span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return false;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return true;</div>
</div>
<div>
<div></div>
<br>
</div>
I'am grabbing the frame with the following code:</div>
<div><br>
</div>
<div>
<div>AVFrame* pic = NULL;</div>
<div>uint8_t* picture_buf = NULL;</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>int size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, _avCodecContext->width, _avCodecContext->height, 1);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>picture_buf = (uint8_t*)(av_malloc(size));</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>pic = av_frame_alloc();</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_image_fill_arrays(pic->data, pic->linesize, picture_buf, AV_PIX_FMT_YUV420P, _avCodecContext->width, _avCodecContext->height, 1);</div>
<div>}</div>
<div><br>
</div>
<div>{ // main loop for grabbing</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>int got_frame = -1;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>while(av_read_frame(_avFormatContext, &packet) >=0 && this->_bThreadRunning == true) </div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if(packet.stream_index == video_stream_index)</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>{<span class="Apple-tab-span" style="white-space:pre">
</span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_pkt_dump_log2(NULL, AV_LOG_TRACE, &packet, 0, stream);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>int bytesDecoded = avcodec_decode_video2(_avCodecContext, pic, &got_frame, &packet);</div>
</div>
<div><br>
<span></span></div>
<div><span><br>
</span></div>
<div><br>
</div>
<div><span>Log:</span></div>
<div><span>
<div>Initialisation:</div>
<div>
<div><span style="font-size: 12pt;">FFMPEG - Before avformat_find_stream_info() pos: 0 bytes read:14600 seeks:0</span><br>
</div>
<div>FFMPEG - All info found</div>
<div>FFMPEG - 0: start_time: 0.000 duration: -9223372036854.775</div>
<div>FFMPEG - stream: start_time: 0.000 duration: -9223372036854.775 bitrate=335232 kb/s</div>
<div>FFMPEG - After avformat_find_stream_info() pos: 1676160 bytes read:1690680 seeks:0 frames:1</div>
<div>FFMPEG - Input #0, rawvideo, from 'tcp://192.168.1.200:50000':</div>
<div>FFMPEG - Duration: </div>
<div>FFMPEG - N/A</div>
<div>FFMPEG - , start: </div>
<div>FFMPEG - 0.000000</div>
<div>FFMPEG - , bitrate: </div>
<div>FFMPEG - 335232 kb/s</div>
<div>FFMPEG - </div>
<div>FFMPEG - Stream #0:0</div>
<div>FFMPEG - , 1, 1/25</div>
<div>FFMPEG - : Video: rawvideo (I420 / 0x30323449), yuv420p, 1552x720, 335232 kb/s</div>
<div>FFMPEG - , </div>
<div>FFMPEG - 25 tbr, </div>
<div>FFMPEG - 25 tbn, </div>
<div>FFMPEG - 25 tbc</div>
<div>FFMPEG - </div>
<div>Packet information:</div>
<div>FFMPEG - stream #0:</div>
<div>FFMPEG - keyframe=1</div>
<div>FFMPEG - duration=0.040</div>
<div>FFMPEG - dts=</div>
<div>FFMPEG - 0.000</div>
<div>FFMPEG - pts=</div>
<div>FFMPEG - 0.000</div>
<div>FFMPEG - </div>
<div>FFMPEG - size=1676160</div>
</div>
<div></div>
<br>
</span></div>
<p></p>
<p><br>
</p>
</div>
</body>
</html>