[FFmpeg-cvslog] avcodec: add ff_samples_to_time_base() convenience function to internal.h

Justin Ruggles git at videolan.org
Tue Feb 21 05:29:42 CET 2012


ffmpeg | branch: master | Justin Ruggles <justin.ruggles at gmail.com> | Tue Feb  7 15:37:45 2012 -0500| [91a28b0e8e4f09a8256727e8a514bf98da81e186] | committer: Justin Ruggles

avcodec: add ff_samples_to_time_base() convenience function to internal.h

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=91a28b0e8e4f09a8256727e8a514bf98da81e186
---

 libavcodec/internal.h    |   11 +++++++++++
 libavcodec/libspeexenc.c |    5 +----
 libavcodec/libvorbis.c   |    5 +++--
 libavcodec/utils.c       |   15 ++++++---------
 4 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index b435a35..bedb2ed 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -26,6 +26,7 @@
 
 #include <stdint.h>
 
+#include "libavutil/mathematics.h"
 #include "libavutil/pixfmt.h"
 #include "avcodec.h"
 
@@ -127,4 +128,14 @@ int avpriv_unlock_avformat(void);
  */
 int ff_alloc_packet(AVPacket *avpkt, int size);
 
+/**
+ * Rescale from sample rate to AVCodecContext.time_base.
+ */
+static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx,
+                                                        int64_t samples)
+{
+    return av_rescale_q(samples, (AVRational){ 1, avctx->sample_rate },
+                        avctx->time_base);
+}
+
 #endif /* AVCODEC_INTERNAL_H */
diff --git a/libavcodec/libspeexenc.c b/libavcodec/libspeexenc.c
index bccf9c8..73a1d4e 100644
--- a/libavcodec/libspeexenc.c
+++ b/libavcodec/libspeexenc.c
@@ -67,7 +67,6 @@
 #include <speex/speex.h>
 #include <speex/speex_header.h>
 #include <speex/speex_stereo.h>
-#include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "internal.h"
@@ -258,9 +257,7 @@ static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size,
     /* write output if all frames for the packet have been encoded */
     if (s->pkt_frame_count == s->frames_per_packet) {
         s->pkt_frame_count = 0;
-        avctx->coded_frame->pts =
-            av_rescale_q(s->next_pts, (AVRational){ 1, avctx->sample_rate },
-                         avctx->time_base);
+        avctx->coded_frame->pts = ff_samples_to_time_base(avctx, s->next_pts);
         s->next_pts += s->pkt_sample_count;
         s->pkt_sample_count = 0;
         if (buf_size > speex_bits_nbytes(&s->bits)) {
diff --git a/libavcodec/libvorbis.c b/libavcodec/libvorbis.c
index 25e6006..4d58fdc 100644
--- a/libavcodec/libvorbis.c
+++ b/libavcodec/libvorbis.c
@@ -29,8 +29,8 @@
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 #include "vorbis.h"
-#include "libavutil/mathematics.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -216,7 +216,8 @@ static int oggvorbis_encode_frame(AVCodecContext *avccontext,
         op2->packet = context->buffer + sizeof(ogg_packet);
 
         l = op2->bytes;
-        avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational) { 1, avccontext->sample_rate }, avccontext->time_base);
+        avccontext->coded_frame->pts = ff_samples_to_time_base(avccontext,
+                                                               op2->granulepos);
         //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
 
         if (l > buf_size) {
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 255406f..2ab3b8e 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -886,9 +886,8 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
         if (!ret && *got_packet_ptr) {
             if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
                 avpkt->pts = frame->pts;
-                avpkt->duration = av_rescale_q(frame->nb_samples,
-                                               (AVRational){ 1, avctx->sample_rate },
-                                               avctx->time_base);
+                avpkt->duration = ff_samples_to_time_base(avctx,
+                                                          frame->nb_samples);
             }
             avpkt->dts = avpkt->pts;
         } else {
@@ -944,9 +943,8 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
                    once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
                    encode2() */
                 if (fs_tmp) {
-                    avpkt->duration = av_rescale_q(avctx->frame_size,
-                                                   (AVRational){ 1, avctx->sample_rate },
-                                                   avctx->time_base);
+                    avpkt->duration = ff_samples_to_time_base(avctx,
+                                                              avctx->frame_size);
                 }
             }
             avpkt->size = ret;
@@ -1018,9 +1016,8 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
         /* fabricate frame pts from sample count.
            this is needed because the avcodec_encode_audio() API does not have
            a way for the user to provide pts */
-        frame->pts = av_rescale_q(avctx->internal->sample_count,
-                                  (AVRational){ 1, avctx->sample_rate },
-                                  avctx->time_base);
+        frame->pts = ff_samples_to_time_base(avctx,
+                                             avctx->internal->sample_count);
         avctx->internal->sample_count += frame->nb_samples;
     } else {
         frame = NULL;



More information about the ffmpeg-cvslog mailing list