blob: 01e06ca51da317ad3393317cc24a0cb9d6c09171 [file] [log] [blame]
Andreas Huber4456da52010-11-09 08:57:45 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Marco Nelissenb636abd2012-03-19 13:49:43 -070017#define LOG_TAG "XINGSEEKER"
18#include <utils/Log.h>
19
Marco Nelissen75226172016-11-16 14:10:52 -080020#include "XINGSeeker.h"
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070021#include <media/stagefright/foundation/avc_utils.h>
Andreas Huber4456da52010-11-09 08:57:45 -080022
Dongwon Kang60761282017-10-09 11:16:48 -070023#include <media/stagefright/foundation/ByteUtils.h>
Marco Nelissen2a243f02018-01-30 08:29:57 -080024#include <media/DataSourceBase.h>
Andreas Huber4456da52010-11-09 08:57:45 -080025
26namespace android {
27
Andreas Huber4456da52010-11-09 08:57:45 -080028XINGSeeker::XINGSeeker()
29 : mDurationUs(-1),
Marco Nelissenb636abd2012-03-19 13:49:43 -070030 mSizeBytes(0),
31 mEncoderDelay(0),
Marco Nelissen5fd7d3a2012-06-13 08:51:00 -070032 mEncoderPadding(0),
33 mTOCValid(false) {
Andreas Huber4456da52010-11-09 08:57:45 -080034}
35
36bool XINGSeeker::getDuration(int64_t *durationUs) {
37 if (mDurationUs < 0) {
38 return false;
39 }
40
41 *durationUs = mDurationUs;
42
43 return true;
44}
45
James Dongc7fc37a2010-11-16 14:04:54 -080046bool XINGSeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
Marco Nelissen28301382018-07-17 16:03:09 -070047 if (mSizeBytes == 0 || mDurationUs < 0) {
Andreas Huber4456da52010-11-09 08:57:45 -080048 return false;
49 }
50
51 float percent = (float)(*timeUs) * 100 / mDurationUs;
52 float fx;
53 if( percent <= 0.0f ) {
54 fx = 0.0f;
55 } else if( percent >= 100.0f ) {
56 fx = 256.0f;
Marco Nelissen28301382018-07-17 16:03:09 -070057 } else if (mTOCValid) {
Andreas Huber4456da52010-11-09 08:57:45 -080058 int a = (int)percent;
59 float fa, fb;
60 if ( a == 0 ) {
61 fa = 0.0f;
62 } else {
Marco Nelissen51024002012-03-14 16:40:11 -070063 fa = (float)mTOC[a-1];
Andreas Huber4456da52010-11-09 08:57:45 -080064 }
65 if ( a < 99 ) {
Marco Nelissen51024002012-03-14 16:40:11 -070066 fb = (float)mTOC[a];
Andreas Huber4456da52010-11-09 08:57:45 -080067 } else {
68 fb = 256.0f;
69 }
70 fx = fa + (fb-fa)*(percent-a);
Marco Nelissen28301382018-07-17 16:03:09 -070071 } else {
72 fx = percent * 2.56f;
Andreas Huber4456da52010-11-09 08:57:45 -080073 }
74
75 *pos = (int)((1.0f/256.0f)*fx*mSizeBytes) + mFirstFramePos;
76
77 return true;
78}
79
Marco Nelissen9e503852012-03-16 07:56:42 -070080// static
Marco Nelissen3d21ae32018-02-16 08:24:08 -080081XINGSeeker *XINGSeeker::CreateFromSource(
Marco Nelissen2a243f02018-01-30 08:29:57 -080082 DataSourceBase *source, off64_t first_frame_pos) {
Marco Nelissen9e503852012-03-16 07:56:42 -070083
Andreas Huber4456da52010-11-09 08:57:45 -080084 uint8_t buffer[4];
85 int offset = first_frame_pos;
86 if (source->readAt(offset, &buffer, 4) < 4) { // get header
Marco Nelissen9e503852012-03-16 07:56:42 -070087 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -080088 }
89 offset += 4;
90
Marco Nelissen9e503852012-03-16 07:56:42 -070091 int header = U32_AT(buffer);;
92 size_t xingframesize = 0;
93 int sampling_rate = 0;
94 int num_channels;
95 int samples_per_frame = 0;
96 if (!GetMPEGAudioFrameSize(header, &xingframesize, &sampling_rate, &num_channels,
97 NULL, &samples_per_frame)) {
98 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -080099 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700100 uint8_t version = (buffer[1] >> 3) & 3;
101
Andreas Huber4456da52010-11-09 08:57:45 -0800102 // determine offset of XING header
Marco Nelissen9e503852012-03-16 07:56:42 -0700103 if(version & 1) { // mpeg1
104 if (num_channels != 1) offset += 32;
Andreas Huber4456da52010-11-09 08:57:45 -0800105 else offset += 17;
Marco Nelissen9e503852012-03-16 07:56:42 -0700106 } else { // mpeg 2 or 2.5
107 if (num_channels != 1) offset += 17;
Andreas Huber4456da52010-11-09 08:57:45 -0800108 else offset += 9;
109 }
110
Marco Nelissenb636abd2012-03-19 13:49:43 -0700111 int xingbase = offset;
112
Andreas Huber4456da52010-11-09 08:57:45 -0800113 if (source->readAt(offset, &buffer, 4) < 4) { // XING header ID
Marco Nelissen9e503852012-03-16 07:56:42 -0700114 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800115 }
116 offset += 4;
117 // Check XING ID
118 if ((buffer[0] != 'X') || (buffer[1] != 'i')
119 || (buffer[2] != 'n') || (buffer[3] != 'g')) {
120 if ((buffer[0] != 'I') || (buffer[1] != 'n')
121 || (buffer[2] != 'f') || (buffer[3] != 'o')) {
Marco Nelissen9e503852012-03-16 07:56:42 -0700122 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800123 }
124 }
125
126 if (source->readAt(offset, &buffer, 4) < 4) { // flags
Marco Nelissen9e503852012-03-16 07:56:42 -0700127 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800128 }
129 offset += 4;
130 uint32_t flags = U32_AT(buffer);
131
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800132 XINGSeeker *seeker = new XINGSeeker;
133 seeker->mFirstFramePos = first_frame_pos + xingframesize;
134
Andreas Huber4456da52010-11-09 08:57:45 -0800135 if (flags & 0x0001) { // Frames field is present
136 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800137 delete seeker;
138 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800139 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700140 int32_t frames = U32_AT(buffer);
Marco Nelissen5fd7d3a2012-06-13 08:51:00 -0700141 // only update mDurationUs if the calculated duration is valid (non zero)
142 // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
143 // return false when called, to indicate that this xing tag does not have the
144 // requested information
145 if (frames) {
146 seeker->mDurationUs = (int64_t)frames * samples_per_frame * 1000000LL / sampling_rate;
147 }
Andreas Huber4456da52010-11-09 08:57:45 -0800148 offset += 4;
149 }
150 if (flags & 0x0002) { // Bytes field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700151 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800152 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700153 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800154 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700155 seeker->mSizeBytes = U32_AT(buffer);
Andreas Huber4456da52010-11-09 08:57:45 -0800156 offset += 4;
157 }
158 if (flags & 0x0004) { // TOC field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700159 if (source->readAt(offset + 1, seeker->mTOC, 99) < 99) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800160 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700161 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800162 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700163 seeker->mTOCValid = true;
Andreas Huber4456da52010-11-09 08:57:45 -0800164 offset += 100;
165 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700166
167#if 0
Andreas Huber4456da52010-11-09 08:57:45 -0800168 if (flags & 0x0008) { // Quality indicator field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700169 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800170 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700171 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800172 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700173 // do something with the quality indicator
174 offset += 4;
Andreas Huber4456da52010-11-09 08:57:45 -0800175 }
Marco Nelissenb636abd2012-03-19 13:49:43 -0700176
177 if (source->readAt(xingbase + 0xaf - 0x24, &buffer, 1) < 1) { // encoding flags
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800178 delete seeker;
Marco Nelissenb636abd2012-03-19 13:49:43 -0700179 return false;
180 }
181
182 ALOGV("nogap preceding: %s, nogap continued in next: %s",
183 (buffer[0] & 0x80) ? "true" : "false",
184 (buffer[0] & 0x40) ? "true" : "false");
Marco Nelissen9e503852012-03-16 07:56:42 -0700185#endif
186
Marco Nelissenb636abd2012-03-19 13:49:43 -0700187 if (source->readAt(xingbase + 0xb1 - 0x24, &buffer, 3) == 3) {
188 seeker->mEncoderDelay = (buffer[0] << 4) + (buffer[1] >> 4);
189 seeker->mEncoderPadding = ((buffer[1] & 0xf) << 8) + buffer[2];
190 }
191
Marco Nelissen9e503852012-03-16 07:56:42 -0700192 return seeker;
Andreas Huber4456da52010-11-09 08:57:45 -0800193}
194
Marco Nelissenb636abd2012-03-19 13:49:43 -0700195int32_t XINGSeeker::getEncoderDelay() {
196 return mEncoderDelay;
197}
198
199int32_t XINGSeeker::getEncoderPadding() {
200 return mEncoderPadding;
201}
202
Andreas Huber4456da52010-11-09 08:57:45 -0800203} // namespace android
204