<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 style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="margin:0px;font-size:12pt;background-color:rgb(255, 255, 255)"><span style="margin:0px;font-size:15px;color:rgb(35, 38, 41);text-align:left;background-color:rgb(255, 255, 255);display:inline !important">Hi, </span></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div style="margin:0px;font-size:12pt;background-color:rgb(255, 255, 255)"><span style="margin:0px;font-size:15px;color:rgb(35, 38, 41);text-align:left;background-color:rgb(255, 255, 255);display:inline !important">I want to add a custom number to either an
 AVFame or an AVPacket before I encode it  and I want to retrieve it back when I decode it. I have tried using the</span><span style="margin:0px;font-size:15px;color:rgb(35, 38, 41);text-align:left;background-color:rgb(255, 255, 255);display:inline !important"> AVFrame/AVPacket
 metadata or side_data but I guess I don't understand how to get it to work. I have also looked into  using</span><span style="margin:0px;font-size:15px;color:rgb(35, 38, 41);text-align:left;background-color:rgb(255, 255, 255);display:inline !important"> AVDictionary/
 AVDictionaryEntry  but nothing so far. The data that I want to store per frame is only a uint32_t. The encoding/decoding of frames work correctly, I have been running it for weeks, here is my encoding piece that suppose to add the "custom number" in, but I'm
 stuck ....</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255, 255, 255)"><span style="margin:0px;font-size:15px;color:rgb(35, 38, 41);text-align:left;background-color:rgb(255, 255, 255);display:inline !important"><br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255, 255, 255)"><span style="margin:0px;font-size:15px;color:rgb(35, 38, 41);text-align:left;background-color:rgb(255, 255, 255);display:inline !important">void MovieEncoder::writeFrame( AVFrame* frame,
 int frameNumber )
<div style="margin:0px">{</div>
<div style="margin:0px">    int result;</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">    if ( s_frameCount >= streamNumberOfFrames)</div>
<div style="margin:0px">        s_frameCount = streamNumberOfFrames;</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">    frame->pts = s_frameCount;</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">    if (m_formatCtx->oformat->flags & AVFMT_NOFILE )</div>
<div style="margin:0px">    {</div>
<div style="margin:0px">        AVPacket pkt;</div>
<div style="margin:0px">        av_init_packet(&pkt);</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">        pkt.flags |= AV_PKT_FLAG_KEY;</div>
<div style="margin:0px">        pkt.stream_index = m_stream->index;</div>
<div style="margin:0px">        pkt.data = frame->data[0];</div>
<div style="margin:0px">        pkt.size = sizeof(AVPicture);</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">        result = av_write_frame( m_formatCtx, &pkt );</div>
<div style="margin:0px">    }</div>
<div style="margin:0px">    else</div>
<div style="margin:0px">    {</div>
<div style="margin:0px">        AVPacket pkt;</div>
<div style="margin:0px">        av_init_packet(&pkt);</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">        AVFrameSideData* numberSideData = av_frame_new_side_data( frame, AV_FRAME_DATA_GOP_TIMECODE, sizeof( uint64_t ))</div>
<div style="margin:0px">//  Here the magic should happen, I think</div>
<div style="margin:0px"></div>
<div style="margin:0px">        if( !numberSideData)</div>
<div style="margin:0px">            return;</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">        result = avcodec_send_frame(m_codecCtx, frame);</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">        checkError( result, "Error encoding video frame: ");</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">        while( result >= 0 )</div>
<div style="margin:0px">        {</div>
<div style="margin:0px">            result = avcodec_receive_packet(m_codecCtx, &pkt);</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">            if (result == AVERROR(EAGAIN) /*|| ret == AVERROR_EOF*/)</div>
<div style="margin:0px">            {</div>
<div style="margin:0px">                Log::printLine("No more packet to write", Log::Debug );</div>
<div style="margin:0px">                return;</div>
<div style="margin:0px">            }</div>
<div style="margin:0px">            else if (result < 0)</div>
<div style="margin:0px">            {</div>
<div style="margin:0px">                  Log::printLine("Codec context of the package is not correct", Log::Debug);</div>
<div style="margin:0px">                  return;</div>
<div style="margin:0px">            }</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">            pkt.stream_index = m_stream->index;</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">            pkt.pts = av_rescale_q( pkt.pts, m_codecCtx->time_base, m_stream->time_base );</div>
<div style="margin:0px">            pkt.dts = av_rescale_q( pkt.dts, m_codecCtx->time_base, m_stream->time_base );</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">//            AVDictionary* dictionary = nullptr;</div>
<div style="margin:0px">//            av_dict_set( &dictionary, metaKey.c_str(),std::to_string( frameNumber ).c_str(), 0 );</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">//            int dictionarySize = 0;</div>
<div style="margin:0px">//            uint8_t* dictionaryData = av_packet_pack_dictionary( dictionary, &dictionarySize );</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">//            av_dict_free( &dictionary );</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">//            av_packet_add_side_data( &pkt, AV_PKT_DATA_STRINGS_METADATA, dictionaryData, dictionarySize );</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">            pkt.duration = int64( 1 / streamFrameRate );</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">            result = av_write_frame( m_formatCtx, &pkt );</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">            s_frameCount++;</div>
<div style="margin:0px">        }</div>
<div style="margin:0px">    }</div>
<div style="margin:0px"><br>
</div>
<div style="margin:0px">//    av_dict_free( &dictionary );</div>
} </span></div>
<br>
</div>
</body>
</html>