<div dir="ltr">Hi,<div><br></div><div>I am implementing ffmpeg as a mex function in matlab as part of a research project (with main focus on the audio, not video like OpenCV, etc). </div><div><br></div><div>I managed to have an initial version of audio encoding working, but I need to clarify few things before considering it usable by others with other codecs that I tried.</div>
<div><br></div><div>1. Newbie questions section:</div><div><div>1.1: Few definitions which the documentation expects are very clear to the developer (which is not this case).</div><div><span class="" style="white-space:pre">        </span>avctx->sample_fmt => is the input or the output format?  (for my current codec I am lucky both input and output formats are the same, but I need to know for other situations).</div>
<div><span class="" style="white-space:pre">    </span>avctx->sample_rate => for the input or for the output? From what I understand it's for output, correct?</div><div><span class="" style="white-space:pre">  </span>avctx->bit_rate => of the input of of the output? From what I understand it's for output, correct?</div>
<div><span class="" style="white-space:pre">    </span>avctx->bits_per_raw_sample => I understand it's for input. Shall I set it, or it defaults to s16 by itself? In the examples I did not find this being set.</div>
<div><span class="" style="white-space:pre">    </span>avctx->frame_size => I understood is for input, correct?</div><div><br></div><div>1.2:<span class="" style="white-space:pre">        </span>how to pass from an external program, things like AVCodecID, which are enum type. It int good enough (size wise) or I should send int64_t to be safe? </div>
<div><br></div><div>1.3:<span class="" style="white-space:pre">       </span>bit_rate seems to be defined as int in the avcodecontext. Is it able to accept higher bit_rate values as well?</div><div><br></div><div>1.4:<span class="" style="white-space:pre">        </span>When is required to flush the encoder and how to do it? Simply reading at the end the delayed frames like this: avcodec_encode_audio2(c, &pkt, NULL, &got_output) means flushing the encoder?</div>
<div><br></div><div>2. bit more advance questions:</div><div>2.1.1 I have a loop parsing an input buffer, shall I call inside the loop av_init_packet(&pkt) each time before avcodec_encode_audio2, or it's enough only once before the loop? </div>
<div>    <span class="" style="white-space:pre">        </span>I am asking because in the avcodec.c example it shows as if I need to run it each time. <a href="http://www.ffmpeg.org/doxygen/trunk/avcodec_8c-example.html#a43">http://www.ffmpeg.org/doxygen/trunk/avcodec_8c-example.html#a43</a> (note: I am focusing on performance of the code as well).</div>
<div>    </div><div>2.1.2   I am writing the output in a buffer, by pointing the pkt.data to the output buffer: pkt.data=&audio_outbuf[audio_outbuf_actual_encoded_size];</div><div>    <span class="" style="white-space:pre">     </span>The question shall I run av_free_packet(&pkt); inside the loop (as in question 2.1.1) ? (of course this time after the avcodec_encode_audio2).</div>
<div>        If so, will av_free_packet-> temper with my output buffer to which pkt.data is pointing to?  </div><div><br></div><div>    <span class="" style="white-space:pre">        </span>Currently, when I do: av_free(audio_outbuf) at the end of the application, and all seems fine, but just to make sure.</div>
<div><br></div><div>2.2 amrwb (like other many codecs), encoding of each frame depends on the adjacent frames, is there something special I should to while feeding the encoder, in order to keep the "context"?</div>
<div><br></div><div>AVFrame *frame = av_frame_alloc();</div><div>avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,(const uint8_t*)audio_bufin_single_frame, audio_bufin_single_frame_buffer_size, 0);</div><div>
<br></div><div>3. Is there some magic way of allocating the right size of the required output buffer? As of now, I am using an non-VBR codec, I encode the 1st frame, I get the size of the 1st output package and I multiply with the number of frames I have to encode in total. Is there some better option? </div>
<div><br></div><div>4. I have set av_log_set_level(AV_LOG_VERBOSE), but none of the methods I call from the ffmpeg libraries show any debug output. Should I expect them to say anything or not?</div></div><div><br></div><div>
Initial version (as of now only for audio encoding) can be found at: <a href="https://github.com/ReSearchITEng/ffmpeg_matlab">https://github.com/ReSearchITEng/ffmpeg_matlab</a> (utils.h-> main_encode2 is "the" function) (Open Source, suggestions and contributions are welcome).<br>
</div><div>I removed the matlab sections, so I can compile and debug it in a IDE.</div><div><br></div><div>Thanks in advance for replies,</div><div>Sebastian</div></div>