blob: 81ed9c66a2b6be9cd3e0ce69c963b46dcf67793a [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
Andreas Huber4456da52010-11-09 08:57:45 -080020#include "include/XINGSeeker.h"
Marco Nelissen9e503852012-03-16 07:56:42 -070021#include "include/avc_utils.h"
Andreas Huber4456da52010-11-09 08:57:45 -080022
23#include <media/stagefright/DataSource.h>
24#include <media/stagefright/Utils.h>
25
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
79sp<XINGSeeker> XINGSeeker::CreateFromSource(
80 const sp<DataSource> &source, off64_t first_frame_pos) {
81 sp<XINGSeeker> seeker = new XINGSeeker;
82
83 seeker->mFirstFramePos = first_frame_pos;
84
Andreas Huber4456da52010-11-09 08:57:45 -080085 uint8_t buffer[4];
86 int offset = first_frame_pos;
87 if (source->readAt(offset, &buffer, 4) < 4) { // get header
Marco Nelissen9e503852012-03-16 07:56:42 -070088 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -080089 }
90 offset += 4;
91
Marco Nelissen9e503852012-03-16 07:56:42 -070092 int header = U32_AT(buffer);;
93 size_t xingframesize = 0;
94 int sampling_rate = 0;
95 int num_channels;
96 int samples_per_frame = 0;
97 if (!GetMPEGAudioFrameSize(header, &xingframesize, &sampling_rate, &num_channels,
98 NULL, &samples_per_frame)) {
99 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800100 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700101 seeker->mFirstFramePos += xingframesize;
102
103 uint8_t version = (buffer[1] >> 3) & 3;
104
Andreas Huber4456da52010-11-09 08:57:45 -0800105 // determine offset of XING header
Marco Nelissen9e503852012-03-16 07:56:42 -0700106 if(version & 1) { // mpeg1
107 if (num_channels != 1) offset += 32;
Andreas Huber4456da52010-11-09 08:57:45 -0800108 else offset += 17;
Marco Nelissen9e503852012-03-16 07:56:42 -0700109 } else { // mpeg 2 or 2.5
110 if (num_channels != 1) offset += 17;
Andreas Huber4456da52010-11-09 08:57:45 -0800111 else offset += 9;
112 }
113
Marco Nelissenb636abd2012-03-19 13:49:43 -0700114 int xingbase = offset;
115
Andreas Huber4456da52010-11-09 08:57:45 -0800116 if (source->readAt(offset, &buffer, 4) < 4) { // XING header ID
Marco Nelissen9e503852012-03-16 07:56:42 -0700117 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800118 }
119 offset += 4;
120 // Check XING ID
121 if ((buffer[0] != 'X') || (buffer[1] != 'i')
122 || (buffer[2] != 'n') || (buffer[3] != 'g')) {
123 if ((buffer[0] != 'I') || (buffer[1] != 'n')
124 || (buffer[2] != 'f') || (buffer[3] != 'o')) {
Marco Nelissen9e503852012-03-16 07:56:42 -0700125 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800126 }
127 }
128
129 if (source->readAt(offset, &buffer, 4) < 4) { // flags
Marco Nelissen9e503852012-03-16 07:56:42 -0700130 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800131 }
132 offset += 4;
133 uint32_t flags = U32_AT(buffer);
134
135 if (flags & 0x0001) { // Frames field is present
136 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen9e503852012-03-16 07:56:42 -0700137 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800138 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700139 int32_t frames = U32_AT(buffer);
Marco Nelissen5fd7d3a2012-06-13 08:51:00 -0700140 // only update mDurationUs if the calculated duration is valid (non zero)
141 // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
142 // return false when called, to indicate that this xing tag does not have the
143 // requested information
144 if (frames) {
145 seeker->mDurationUs = (int64_t)frames * samples_per_frame * 1000000LL / sampling_rate;
146 }
Andreas Huber4456da52010-11-09 08:57:45 -0800147 offset += 4;
148 }
149 if (flags & 0x0002) { // Bytes field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700150 if (source->readAt(offset, buffer, 4) < 4) {
151 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) {
158 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800159 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700160 seeker->mTOCValid = true;
Andreas Huber4456da52010-11-09 08:57:45 -0800161 offset += 100;
162 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700163
164#if 0
Andreas Huber4456da52010-11-09 08:57:45 -0800165 if (flags & 0x0008) { // Quality indicator field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700166 if (source->readAt(offset, buffer, 4) < 4) {
167 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800168 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700169 // do something with the quality indicator
170 offset += 4;
Andreas Huber4456da52010-11-09 08:57:45 -0800171 }
Marco Nelissenb636abd2012-03-19 13:49:43 -0700172
173 if (source->readAt(xingbase + 0xaf - 0x24, &buffer, 1) < 1) { // encoding flags
174 return false;
175 }
176
177 ALOGV("nogap preceding: %s, nogap continued in next: %s",
178 (buffer[0] & 0x80) ? "true" : "false",
179 (buffer[0] & 0x40) ? "true" : "false");
Marco Nelissen9e503852012-03-16 07:56:42 -0700180#endif
181
Marco Nelissenb636abd2012-03-19 13:49:43 -0700182 if (source->readAt(xingbase + 0xb1 - 0x24, &buffer, 3) == 3) {
183 seeker->mEncoderDelay = (buffer[0] << 4) + (buffer[1] >> 4);
184 seeker->mEncoderPadding = ((buffer[1] & 0xf) << 8) + buffer[2];
185 }
186
Marco Nelissen9e503852012-03-16 07:56:42 -0700187 return seeker;
Andreas Huber4456da52010-11-09 08:57:45 -0800188}
189
Marco Nelissenb636abd2012-03-19 13:49:43 -0700190int32_t XINGSeeker::getEncoderDelay() {
191 return mEncoderDelay;
192}
193
194int32_t XINGSeeker::getEncoderPadding() {
195 return mEncoderPadding;
196}
197
Andreas Huber4456da52010-11-09 08:57:45 -0800198} // namespace android
199