[FFmpeg-trac] #9228(avcodec:closed): multiple output in single C/libavcodec xcode process offsets "start" in output files

FFmpeg trac at avcodec.org
Tue May 11 11:53:28 EEST 2021


#9228: multiple output in single C/libavcodec xcode process offsets "start" in
output files
------------------------------------+-----------------------------------
             Reporter:  Ray         |                    Owner:  (none)
                 Type:  defect      |                   Status:  closed
             Priority:  normal      |                Component:  avcodec
              Version:  git-master  |               Resolution:  invalid
             Keywords:  AAC         |               Blocked By:
             Blocking:              |  Reproduced by developer:  0
Analyzed by developer:  0           |
------------------------------------+-----------------------------------
Comment (by Ray):

 Replying to [comment:5 mkver]:
 > The reason for what you are experiencing is the use of the static pts
 variable which does not get reset when switching to a new output file. The
 reason you are not experiencing this with mp3 is that the mp3 muxer does
 not retain the pts (probably because the file format does not allow this).

 Thank you!  I had based my appl on the examples which has this.  Will
 submit a patch to correct this but for anyone that might search this in
 future:

 {{{
 diff --git a/doc/examples/transcode_aac.c b/doc/examples/transcode_aac.c
 index 711076b5a5..7f6148fa15 100644
 --- a/doc/examples/transcode_aac.c
 +++ b/doc/examples/transcode_aac.c
 @@ -648,8 +648,6 @@ static int init_output_frame(AVFrame **frame,
      return 0;
  }

 -/* Global timestamp for the audio frames. */
 -static int64_t pts = 0;

  /**
   * Encode one frame worth of audio to the output file.
 @@ -663,6 +661,7 @@ static int64_t pts = 0;
  static int encode_audio_frame(AVFrame *frame,
                                AVFormatContext *output_format_context,
                                AVCodecContext *output_codec_context,
 +                             int64_t* pts,
                                int *data_present)
  {
      /* Packet used for temporary storage. */
 @@ -675,8 +674,8 @@ static int encode_audio_frame(AVFrame *frame,

      /* Set a timestamp based on the sample rate for the container. */
      if (frame) {
 -        frame->pts = pts;
 -        pts += frame->nb_samples;
 +        frame->pts = *pts;
 +        *pts += frame->nb_samples;
      }

      /* Send the audio frame stored in the temporary packet to the
 encoder.
 @@ -685,7 +684,6 @@ static int encode_audio_frame(AVFrame *frame,
      /* The encoder signals that it has nothing more to encode. */
      if (error == AVERROR_EOF) {
          error = 0;
 -        goto cleanup;
      } else if (error < 0) {
          fprintf(stderr, "Could not send packet for encoding (error
 '%s')\n",
                  av_err2str(error));
 @@ -735,7 +733,8 @@ cleanup:
   */
  static int load_encode_and_write(AVAudioFifo *fifo,
                                   AVFormatContext *output_format_context,
 -                                 AVCodecContext *output_codec_context)
 +                                 AVCodecContext *output_codec_context,
 +                                 int64_t* pts)
  {
      /* Temporary storage of the output samples of the frame written to
 the file. */
      AVFrame *output_frame;
 @@ -760,7 +759,7 @@ static int load_encode_and_write(AVAudioFifo *fifo,

      /* Encode one frame worth of audio samples. */
      if (encode_audio_frame(output_frame, output_format_context,
 -                           output_codec_context, &data_written)) {
 +                           output_codec_context, pts, &data_written)) {
          av_frame_free(&output_frame);
          return AVERROR_EXIT;
      }
 @@ -791,6 +790,8 @@ int main(int argc, char **argv)
      SwrContext *resample_context = NULL;
      AVAudioFifo *fifo = NULL;
      int ret = AVERROR_EXIT;
 +    /* timestamp for the audio frames. */
 +    int64_t pts = 0;

      if (argc != 3) {
          fprintf(stderr, "Usage: %s <input file> <output file>\n",
 argv[0]);
 @@ -851,7 +852,7 @@ int main(int argc, char **argv)
              /* Take one frame worth of audio samples from the FIFO
 buffer,
               * encode it and write it to the output file. */
              if (load_encode_and_write(fifo, output_format_context,
 -                                      output_codec_context))
 +                                      output_codec_context, &pts))
                  goto cleanup;

          /* If we are at the end of the input file and have encoded
 @@ -862,7 +863,7 @@ int main(int argc, char **argv)
              do {
                  data_written = 0;
                  if (encode_audio_frame(NULL, output_format_context,
 -                                       output_codec_context,
 &data_written))
 +                                       output_codec_context, &pts,
 &data_written))
                      goto cleanup;
              } while (data_written);
              break;
 }}}
-- 
Ticket URL: <https://trac.ffmpeg.org/ticket/9228#comment:6>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker


More information about the FFmpeg-trac mailing list