AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OutputStream.cpp
Go to the documentation of this file.
1 #include "OutputStream.hpp"
2 
4 
5 #include <cassert>
6 
7 namespace avtranscoder
8 {
9 
10 OutputStream::OutputStream(OutputFile& outputFile, const size_t streamIndex)
11  : IOutputStream()
12  , _outputFile(outputFile)
13  , _outputAVStream(outputFile.getFormatContext().getAVStream(streamIndex))
14  , _streamIndex(streamIndex)
15  , _wrappedPacketsDuration(0)
16  , _lastWrappedPacketDuration(0)
17  , _isPTSGenerated(false)
18 {
19 }
20 
22 {
23  const AVFrac& outputPTS = _outputAVStream.pts;
24  const AVRational& outputTimeBase = _outputAVStream.time_base;
25 
26  // check floating point exception
27  if(outputTimeBase.den == 0 || outputPTS.den == 0)
28  {
29  LOG_WARN("Cannot compute stream duration of output stream at index " << _streamIndex)
30  return 0.f;
31  }
32 
34  return av_q2d(outputTimeBase) * _wrappedPacketsDuration;
35 #if AVTRANSCODER_FFMPEG_DEPENDENCY && LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(55, 40, 100)
36  // returns the pts of the last muxed packet, converted from timebase to seconds
37  return av_q2d(outputTimeBase) * av_stream_get_end_pts(&_outputAVStream);
38 #else
39  return av_q2d(outputTimeBase) * (outputPTS.val + (outputPTS.num / outputPTS.den));
40 #endif
41 }
42 
44 {
45  return _outputAVStream.nb_frames;
46 }
47 
49 {
50  const AVFrac& outputPTS = _outputAVStream.pts;
51  return (outputPTS.val + (outputPTS.num / outputPTS.den));
52 }
53 
55 {
56  // If stream PTS will be generated at rewrap time
57  if(!_isPTSGenerated && (data.getAVPacket().pts == 0 || data.getAVPacket().pts == AV_NOPTS_VALUE) &&
58  data.getAVPacket().dts == AV_NOPTS_VALUE)
59  {
60  LOG_WARN("PTS of output stream " << _streamIndex << " is generated at rewrap time.")
61  _isPTSGenerated = true;
62  }
63 
64  // wrap packet
66 
67  // Store duration of the last packet wrapped
68  if(data.getAVPacket().duration != 0 && data.getAVPacket().duration != _lastWrappedPacketDuration)
69  _lastWrappedPacketDuration = data.getAVPacket().duration;
70 
71  // Append duration of the packet to the stream
72  if(data.getAVPacket().duration)
73  _wrappedPacketsDuration += data.getAVPacket().duration;
74  else
75  {
76  switch(_outputAVStream.codec->codec_type)
77  {
78  case AVMEDIA_TYPE_VIDEO:
79  {
81  break;
82  }
83  case AVMEDIA_TYPE_AUDIO:
84  {
85  Rational audioPacketDuration;
86  audioPacketDuration.num = 0;
87  audioPacketDuration.den = 0;
88  const int frame_size = av_get_audio_frame_duration(_outputAVStream.codec, data.getSize());
89  if(frame_size <= 0 || _outputAVStream.codec->sample_rate <= 0)
90  break;
91  audioPacketDuration.num = frame_size;
92  audioPacketDuration.den = _outputAVStream.codec->sample_rate;
93  _wrappedPacketsDuration += av_rescale(1, audioPacketDuration.num * (int64_t)_outputAVStream.time_base.den *
94  _outputAVStream.codec->ticks_per_frame,
95  audioPacketDuration.den * (int64_t)_outputAVStream.time_base.num);
96  break;
97  }
98  default:
99  break;
100  }
101  }
102 
103  return status;
104 }
105 }
size_t _streamIndex
Index of the stream in the output file.
size_t getNbFrames() const
If audio stream, returns number of packets.
IOutputStream::EWrappingStatus wrap(const CodedData &data)
Wrap a packet of data.
OutputStream(OutputFile &outputFile, const size_t streamIndex)
OutputFile & _outputFile
Has link (no ownership)
Outputfile is the default implentation of wrapper which uses LibAV/FFMpeg.
Definition: OutputFile.hpp:17
EWrappingStatus
define wrapping result status
AVPacket & getAVPacket()
Definition: CodedData.hpp:78
IOutputStream::EWrappingStatus wrap(const CodedData &data, const size_t streamIndex)
Wrap a packet of data in the output ressource.
Definition: OutputFile.cpp:166
#define LOG_WARN(...)
Definition: log.hpp:29
const AVStream & _outputAVStream
Has link (no ownership)
size_t getSize() const
Definition: CodedData.hpp:70
int getStreamPTS() const
Get current AVStream PTS.
This class describes coded data.
Definition: CodedData.hpp:18
float getStreamDuration() const
AVRational Rational
Definition: common.hpp:55