Hello everybody,<div><br></div><div>since I'm trying to understand how to properly manipulate mp4 formatted files containing h264 coded content, I resorted on the "output-example.c" file found inside Libav 0.7.3 package.</div>
<div>To understand the very basics of how to properly setup the output contexts, formats and codecs for h264 + mp4, I wrote a simple program that does just the following three steps:</div><div><br></div><div>1) It opens the source mp4 file, encoded with h264, and saves the contexts of interest in temporary variables.</div>
<div>2) It opens the target output contexts, configuring them exactly as in the respective source context (with the exception of pointer variables).</div><div>3) It performs a packet-by-packet read over the source file, followed by a copy of what has been read to the output file, as reported in the following code:</div>
<div><br></div><div><div> AVPacket readPacket;</div><div> AVPacket outputPacket;</div><div> AVStream currentStream;</div><div> int returnCode = 0;</div><div><br></div><div> currentStream = *(g_outputFormatContext->streams[0]);</div>
<div><br></div><div> while (1)</div><div> {</div><div> returnCode = av_read_frame(g_inputFormatContext, &readPacket);</div><div> if (returnCode != 0)</div><div> {</div><div> /* EOF has been reached - exit from the loop */</div>
<div> av_close_input_file(g_inputFormatContext);</div><div> break;</div><div> }</div><div><br></div><div> /* EOF has NOT been reached, continue */</div><div> av_init_packet(&outputPacket);</div>
<div><br></div><div> outputPacket.pts = readPacket.pts;</div><div> outputPacket.dts = readPacket.dts;</div><div> outputPacket.stream_index= currentStream.index;</div><div> outputPacket.data= readPacket.data;</div>
<div> outputPacket.size= readPacket.size;</div><div><br></div><div> returnCode = av_write_frame(g_outputFormatContext, &outputPacket);</div><div> if (returnCode != 0)</div><div> {</div><div> printf("Error on write - exiting\n");</div>
<div> exit(1);</div><div> }</div><div><br></div><div> av_free_packet(&readPacket);</div><div> av_free_packet(&outputPacket);</div><div> }</div><div><br></div><div> /* Close everything */</div>
<div> av_write_trailer(g_outputFormatContext);</div><div> if (!(g_outputFormat->flags & AVFMT_NOFILE))</div><div> {</div><div> /* close the output file */</div><div> avio_close(g_outputFormatContext->pb);</div>
<div> }</div><div> av_free(g_outputFormatContext);</div></div><div><br></div><div><br></div><div>Even if this code is very simple (i.e., it just copies the content of a file into another one), it does not provide satisfactory results, since the output file can't be played.</div>
<div>Please, could someone tell me if the previous code is "reasonable" or not??</div><div><br></div><div>Moreover, by comparing with "ffprobe -show_streams" the original file with the copy one, width, height and other important parameters look as if they have not been properly copied, even if they have been set with:</div>
<div><br></div><div><div>outputStream->codec->bit_rate = g_inputCodecContext->bit_rate;</div><div>outputStream->codec->width = g_inputCodecContext->width;</div><div>outputStream->codec->height = g_inputCodecContext->height;</div>
<div>outputStream->codec->has_b_frames = g_inputCodecContext->has_b_frames;</div><div>outputStream->codec->sample_aspect_ratio = outputStream->sample_aspect_ratio; // WARNING</div><div>outputStream->codec->level = g_inputCodecContext->level;</div>
</div><div><br></div><div>where g_inputCodecContext is the AVCodecContext of the original file.</div><div>Is it possible that AVPacket settings could interfere with the stream and format's configurations??</div><div><br>
</div><div>ANY suggestion is very welcome.</div><div><br></div><div>Thank you in advance,</div><div>kind regards.</div><div><br></div><div>Alessandro</div><div><div><br></div>-- <br>______________________________________________<br>
<br>Ing. Alessandro Paganelli, Ph. D. Student, 25th Cycle<br><br>Department of Information Engineering<br>University of Modena and Reggio Emilia<br>via Vignolese 905, 41125 Modena (MO), ITALY<br><br>E-mail: <a href="mailto:alessandro.paganelli@unimore.it">alessandro.paganelli@unimore.it</a><br>
</div>