3
3
#include < AvTranscoder/properties/util.hpp>
4
4
#include < AvTranscoder/properties/JsonWriter.hpp>
5
5
#include < AvTranscoder/properties/FileProperties.hpp>
6
- #include < AvTranscoder/data/decoded/Frame.hpp>
7
6
8
7
#include < stdexcept>
9
8
@@ -59,103 +58,6 @@ size_t StreamProperties::getStreamId() const
59
58
return _formatContext->streams [_streamIndex]->id ;
60
59
}
61
60
62
- size_t StreamProperties::getBitRate () const
63
- {
64
- if (!_codecContext)
65
- throw std::runtime_error (" unknown codec context" );
66
-
67
- // return bit rate of stream if present or VBR mode
68
- if (_codecContext->bit_rate || _codecContext->rc_max_rate )
69
- return _codecContext->bit_rate ;
70
-
71
- LOG_WARN (" The bitrate of the stream '" << _streamIndex << " ' of file '" << _formatContext->filename << " ' is unknown." )
72
-
73
- const AVMediaType streamType = getStreamType ();
74
-
75
- // compute audio bitrate
76
- if (streamType == AVMEDIA_TYPE_AUDIO)
77
- {
78
- LOG_INFO (" Compute the audio bitrate (suppose PCM audio data)." )
79
-
80
- // warning: this is the way to compute bit rate of PCM audio data
81
- const int bitsPerSample = av_get_bits_per_sample (_codecContext->codec_id ); // 0 if unknown for the given codec
82
- return _codecContext->sample_rate * _codecContext->channels * bitsPerSample;
83
- }
84
-
85
- // compute video bitrate
86
- if (streamType == AVMEDIA_TYPE_VIDEO)
87
- {
88
- LOG_INFO (" Compute the video bitrate by decoding the first GOP." )
89
-
90
- if (!_formatContext || !_codec)
91
- throw std::runtime_error (" cannot compute bit rate: unknown format or codec context" );
92
- if (!_codecContext->width || !_codecContext->height )
93
- throw std::runtime_error (" cannot compute bit rate: invalid frame size" );
94
-
95
- // discard no frame type when decode
96
- _codecContext->skip_frame = AVDISCARD_NONE;
97
-
98
- Frame frame;
99
- AVPacket pkt;
100
- av_init_packet (&pkt);
101
- avcodec_open2 (_codecContext, _codec, NULL );
102
-
103
- int gotFrame = 0 ;
104
- size_t nbDecodedFrames = 0 ;
105
- int gopFramesSize = 0 ;
106
- int positionOfFirstKeyFrame = -1 ;
107
- int positionOfLastKeyFrame = -1 ;
108
-
109
- while (!av_read_frame (const_cast <AVFormatContext*>(_formatContext), &pkt))
110
- {
111
- if (pkt.stream_index == (int )_streamIndex)
112
- {
113
- avcodec_decode_video2 (_codecContext, &frame.getAVFrame (), &gotFrame, &pkt);
114
- if (gotFrame)
115
- {
116
- // check distance between key frames
117
- AVFrame& avFrame = frame.getAVFrame ();
118
- if (avFrame.pict_type == AV_PICTURE_TYPE_I)
119
- {
120
- if (positionOfFirstKeyFrame == -1 )
121
- positionOfFirstKeyFrame = nbDecodedFrames;
122
- else
123
- positionOfLastKeyFrame = nbDecodedFrames;
124
- }
125
- ++nbDecodedFrames;
126
-
127
- // added size of all frames of the same gop
128
- if (positionOfLastKeyFrame == -1 )
129
- {
130
- #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(54, 7, 100)
131
- gopFramesSize += frame.getEncodedSize ();
132
- #else
133
- gopFramesSize += pkt.size ;
134
- #endif
135
- }
136
- }
137
- }
138
- av_free_packet (&pkt);
139
- if (positionOfFirstKeyFrame != -1 && positionOfLastKeyFrame != -1 )
140
- break ;
141
- }
142
- // Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext itself)
143
- avcodec_close (_codecContext);
144
-
145
- // Returns at the beginning of the stream
146
- const_cast <FormatContext*>(&_fileProperties->getFormatContext ())->seek (0 , AVSEEK_FLAG_BYTE);
147
-
148
- const size_t gopSize = positionOfLastKeyFrame - positionOfFirstKeyFrame;
149
- if (gopSize > 0 )
150
- {
151
- const float fps = av_q2d (_formatContext->streams [_streamIndex]->avg_frame_rate );
152
- return (gopFramesSize / gopSize) * 8 * fps;
153
- }
154
- }
155
-
156
- return 0 ;
157
- }
158
-
159
61
Rational StreamProperties::getTimeBase () const
160
62
{
161
63
if (!_formatContext)
@@ -170,16 +72,6 @@ float StreamProperties::getDuration() const
170
72
if (duration == (size_t )AV_NOPTS_VALUE)
171
73
{
172
74
LOG_WARN (" The duration of the stream '" << _streamIndex << " ' of file '" << _formatContext->filename << " ' is unknown." )
173
- if (_fileProperties->isRawFormat ())
174
- {
175
- LOG_INFO (" Get the stream bitrate to compute the duration." )
176
- const size_t bitRate = getBitRate ();
177
- if (bitRate)
178
- {
179
- LOG_INFO (" Get the file size to compute the duration." )
180
- return _fileProperties->getFileSize () / bitRate * 8 ;
181
- }
182
- }
183
75
return 0 ;
184
76
}
185
77
return av_q2d (timeBase) * duration;
@@ -247,7 +139,6 @@ PropertyVector& StreamProperties::fillVector(PropertyVector& data) const
247
139
{
248
140
addProperty (data, " streamId" , &StreamProperties::getStreamId);
249
141
addProperty (data, " streamIndex" , &StreamProperties::getStreamIndex);
250
- addProperty (data, " bitRate" , &StreamProperties::getBitRate);
251
142
addProperty (data, " timeBase" , &StreamProperties::getTimeBase);
252
143
addProperty (data, " duration" , &StreamProperties::getDuration);
253
144
addProperty (data, " codecId" , &StreamProperties::getCodecId);
0 commit comments