blob: 95ca556b3e3d657b1a122436e4add35c41f89c8a [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 Nelissen51024002012-03-14 16:40:11 -070047 if (mSizeBytes == 0 || !mTOCValid || 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;
57 } else {
58 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);
71 }
72
73 *pos = (int)((1.0f/256.0f)*fx*mSizeBytes) + mFirstFramePos;
74
75 return true;
76}
77
Marco Nelissen9e503852012-03-16 07:56:42 -070078// static
Marco Nelissen3d21ae32018-02-16 08:24:08 -080079XINGSeeker *XINGSeeker::CreateFromSource(
Marco Nelissen2a243f02018-01-30 08:29:57 -080080 DataSourceBase *source, off64_t first_frame_pos) {
Marco Nelissen9e503852012-03-16 07:56:42 -070081
Andreas Huber4456da52010-11-09 08:57:45 -080082 uint8_t buffer[4];
83 int offset = first_frame_pos;
84 if (source->readAt(offset, &buffer, 4) < 4) { // get header
Marco Nelissen9e503852012-03-16 07:56:42 -070085 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -080086 }
87 offset += 4;
88
Marco Nelissen9e503852012-03-16 07:56:42 -070089 int header = U32_AT(buffer);;
90 size_t xingframesize = 0;
91 int sampling_rate = 0;
92 int num_channels;
93 int samples_per_frame = 0;
94 if (!GetMPEGAudioFrameSize(header, &xingframesize, &sampling_rate, &num_channels,
95 NULL, &samples_per_frame)) {
96 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -080097 }
Marco Nelissen9e503852012-03-16 07:56:42 -070098 uint8_t version = (buffer[1] >> 3) & 3;
99
Andreas Huber4456da52010-11-09 08:57:45 -0800100 // determine offset of XING header
Marco Nelissen9e503852012-03-16 07:56:42 -0700101 if(version & 1) { // mpeg1
102 if (num_channels != 1) offset += 32;
Andreas Huber4456da52010-11-09 08:57:45 -0800103 else offset += 17;
Marco Nelissen9e503852012-03-16 07:56:42 -0700104 } else { // mpeg 2 or 2.5
105 if (num_channels != 1) offset += 17;
Andreas Huber4456da52010-11-09 08:57:45 -0800106 else offset += 9;
107 }
108
Marco Nelissenb636abd2012-03-19 13:49:43 -0700109 int xingbase = offset;
110
Andreas Huber4456da52010-11-09 08:57:45 -0800111 if (source->readAt(offset, &buffer, 4) < 4) { // XING header ID
Marco Nelissen9e503852012-03-16 07:56:42 -0700112 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800113 }
114 offset += 4;
115 // Check XING ID
116 if ((buffer[0] != 'X') || (buffer[1] != 'i')
117 || (buffer[2] != 'n') || (buffer[3] != 'g')) {
118 if ((buffer[0] != 'I') || (buffer[1] != 'n')
119 || (buffer[2] != 'f') || (buffer[3] != 'o')) {
Marco Nelissen9e503852012-03-16 07:56:42 -0700120 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800121 }
122 }
123
124 if (source->readAt(offset, &buffer, 4) < 4) { // flags
Marco Nelissen9e503852012-03-16 07:56:42 -0700125 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800126 }
127 offset += 4;
128 uint32_t flags = U32_AT(buffer);
129
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800130 XINGSeeker *seeker = new XINGSeeker;
131 seeker->mFirstFramePos = first_frame_pos + xingframesize;
132
Andreas Huber4456da52010-11-09 08:57:45 -0800133 if (flags & 0x0001) { // Frames field is present
134 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800135 delete seeker;
136 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800137 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700138 int32_t frames = U32_AT(buffer);
Marco Nelissen5fd7d3a2012-06-13 08:51:00 -0700139 // only update mDurationUs if the calculated duration is valid (non zero)
140 // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
141 // return false when called, to indicate that this xing tag does not have the
142 // requested information
143 if (frames) {
144 seeker->mDurationUs = (int64_t)frames * samples_per_frame * 1000000LL / sampling_rate;
145 }
Andreas Huber4456da52010-11-09 08:57:45 -0800146 offset += 4;
147 }
148 if (flags & 0x0002) { // Bytes field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700149 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800150 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700151 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800152 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700153 seeker->mSizeBytes = U32_AT(buffer);
Andreas Huber4456da52010-11-09 08:57:45 -0800154 offset += 4;
155 }
156 if (flags & 0x0004) { // TOC field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700157 if (source->readAt(offset + 1, seeker->mTOC, 99) < 99) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800158 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700159 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800160 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700161 seeker->mTOCValid = true;
Andreas Huber4456da52010-11-09 08:57:45 -0800162 offset += 100;
163 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700164
165#if 0
Andreas Huber4456da52010-11-09 08:57:45 -0800166 if (flags & 0x0008) { // Quality indicator field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700167 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800168 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700169 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800170 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700171 // do something with the quality indicator
172 offset += 4;
Andreas Huber4456da52010-11-09 08:57:45 -0800173 }
Marco Nelissenb636abd2012-03-19 13:49:43 -0700174
175 if (source->readAt(xingbase + 0xaf - 0x24, &buffer, 1) < 1) { // encoding flags
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800176 delete seeker;
Marco Nelissenb636abd2012-03-19 13:49:43 -0700177 return false;
178 }
179
180 ALOGV("nogap preceding: %s, nogap continued in next: %s",
181 (buffer[0] & 0x80) ? "true" : "false",
182 (buffer[0] & 0x40) ? "true" : "false");
Marco Nelissen9e503852012-03-16 07:56:42 -0700183#endif
184
Marco Nelissenb636abd2012-03-19 13:49:43 -0700185 if (source->readAt(xingbase + 0xb1 - 0x24, &buffer, 3) == 3) {
186 seeker->mEncoderDelay = (buffer[0] << 4) + (buffer[1] >> 4);
187 seeker->mEncoderPadding = ((buffer[1] & 0xf) << 8) + buffer[2];
188 }
189
Marco Nelissen9e503852012-03-16 07:56:42 -0700190 return seeker;
Andreas Huber4456da52010-11-09 08:57:45 -0800191}
192
Marco Nelissenb636abd2012-03-19 13:49:43 -0700193int32_t XINGSeeker::getEncoderDelay() {
194 return mEncoderDelay;
195}
196
197int32_t XINGSeeker::getEncoderPadding() {
198 return mEncoderPadding;
199}
200
Andreas Huber4456da52010-11-09 08:57:45 -0800201} // namespace android
202