blob: 9f1fd7a91b345d633ebdb5c19041e04cf086e4dc [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 Nelissencec44d02018-06-17 22:21:09 -070024
25#include <media/MediaExtractorPluginApi.h>
26#include <media/MediaExtractorPluginHelper.h>
Andreas Huber4456da52010-11-09 08:57:45 -080027
28namespace android {
29
Andreas Huber4456da52010-11-09 08:57:45 -080030XINGSeeker::XINGSeeker()
31 : mDurationUs(-1),
Marco Nelissenb636abd2012-03-19 13:49:43 -070032 mSizeBytes(0),
33 mEncoderDelay(0),
Marco Nelissen5fd7d3a2012-06-13 08:51:00 -070034 mEncoderPadding(0),
35 mTOCValid(false) {
Andreas Huber4456da52010-11-09 08:57:45 -080036}
37
38bool XINGSeeker::getDuration(int64_t *durationUs) {
39 if (mDurationUs < 0) {
40 return false;
41 }
42
43 *durationUs = mDurationUs;
44
45 return true;
46}
47
James Dongc7fc37a2010-11-16 14:04:54 -080048bool XINGSeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) {
Marco Nelissen28301382018-07-17 16:03:09 -070049 if (mSizeBytes == 0 || mDurationUs < 0) {
Andreas Huber4456da52010-11-09 08:57:45 -080050 return false;
51 }
52
53 float percent = (float)(*timeUs) * 100 / mDurationUs;
54 float fx;
55 if( percent <= 0.0f ) {
56 fx = 0.0f;
57 } else if( percent >= 100.0f ) {
58 fx = 256.0f;
Marco Nelissen28301382018-07-17 16:03:09 -070059 } else if (mTOCValid) {
Andreas Huber4456da52010-11-09 08:57:45 -080060 int a = (int)percent;
61 float fa, fb;
62 if ( a == 0 ) {
63 fa = 0.0f;
64 } else {
Marco Nelissen51024002012-03-14 16:40:11 -070065 fa = (float)mTOC[a-1];
Andreas Huber4456da52010-11-09 08:57:45 -080066 }
67 if ( a < 99 ) {
Marco Nelissen51024002012-03-14 16:40:11 -070068 fb = (float)mTOC[a];
Andreas Huber4456da52010-11-09 08:57:45 -080069 } else {
70 fb = 256.0f;
71 }
72 fx = fa + (fb-fa)*(percent-a);
Marco Nelissen28301382018-07-17 16:03:09 -070073 } else {
74 fx = percent * 2.56f;
Andreas Huber4456da52010-11-09 08:57:45 -080075 }
76
77 *pos = (int)((1.0f/256.0f)*fx*mSizeBytes) + mFirstFramePos;
78
79 return true;
80}
81
Marco Nelissen9e503852012-03-16 07:56:42 -070082// static
Marco Nelissen3d21ae32018-02-16 08:24:08 -080083XINGSeeker *XINGSeeker::CreateFromSource(
Marco Nelissencec44d02018-06-17 22:21:09 -070084 DataSourceHelper *source, off64_t first_frame_pos) {
Marco Nelissen9e503852012-03-16 07:56:42 -070085
Andreas Huber4456da52010-11-09 08:57:45 -080086 uint8_t buffer[4];
87 int offset = first_frame_pos;
88 if (source->readAt(offset, &buffer, 4) < 4) { // get header
Marco Nelissen9e503852012-03-16 07:56:42 -070089 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -080090 }
91 offset += 4;
92
Marco Nelissen9e503852012-03-16 07:56:42 -070093 int header = U32_AT(buffer);;
94 size_t xingframesize = 0;
95 int sampling_rate = 0;
96 int num_channels;
97 int samples_per_frame = 0;
98 if (!GetMPEGAudioFrameSize(header, &xingframesize, &sampling_rate, &num_channels,
99 NULL, &samples_per_frame)) {
100 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800101 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700102 uint8_t version = (buffer[1] >> 3) & 3;
103
Andreas Huber4456da52010-11-09 08:57:45 -0800104 // determine offset of XING header
Marco Nelissen9e503852012-03-16 07:56:42 -0700105 if(version & 1) { // mpeg1
106 if (num_channels != 1) offset += 32;
Andreas Huber4456da52010-11-09 08:57:45 -0800107 else offset += 17;
Marco Nelissen9e503852012-03-16 07:56:42 -0700108 } else { // mpeg 2 or 2.5
109 if (num_channels != 1) offset += 17;
Andreas Huber4456da52010-11-09 08:57:45 -0800110 else offset += 9;
111 }
112
Marco Nelissenb636abd2012-03-19 13:49:43 -0700113 int xingbase = offset;
114
Andreas Huber4456da52010-11-09 08:57:45 -0800115 if (source->readAt(offset, &buffer, 4) < 4) { // XING header ID
Marco Nelissen9e503852012-03-16 07:56:42 -0700116 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800117 }
118 offset += 4;
119 // Check XING ID
120 if ((buffer[0] != 'X') || (buffer[1] != 'i')
121 || (buffer[2] != 'n') || (buffer[3] != 'g')) {
122 if ((buffer[0] != 'I') || (buffer[1] != 'n')
123 || (buffer[2] != 'f') || (buffer[3] != 'o')) {
Marco Nelissen9e503852012-03-16 07:56:42 -0700124 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800125 }
126 }
127
128 if (source->readAt(offset, &buffer, 4) < 4) { // flags
Marco Nelissen9e503852012-03-16 07:56:42 -0700129 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800130 }
131 offset += 4;
132 uint32_t flags = U32_AT(buffer);
133
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800134 XINGSeeker *seeker = new XINGSeeker;
135 seeker->mFirstFramePos = first_frame_pos + xingframesize;
136
Andreas Huber4456da52010-11-09 08:57:45 -0800137 if (flags & 0x0001) { // Frames field is present
138 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800139 delete seeker;
140 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800141 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700142 int32_t frames = U32_AT(buffer);
Marco Nelissen5fd7d3a2012-06-13 08:51:00 -0700143 // only update mDurationUs if the calculated duration is valid (non zero)
144 // otherwise, leave duration at -1 so that getDuration() and getOffsetForTime()
145 // return false when called, to indicate that this xing tag does not have the
146 // requested information
147 if (frames) {
148 seeker->mDurationUs = (int64_t)frames * samples_per_frame * 1000000LL / sampling_rate;
149 }
Andreas Huber4456da52010-11-09 08:57:45 -0800150 offset += 4;
151 }
152 if (flags & 0x0002) { // Bytes field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700153 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800154 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700155 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800156 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700157 seeker->mSizeBytes = U32_AT(buffer);
Andreas Huber4456da52010-11-09 08:57:45 -0800158 offset += 4;
159 }
160 if (flags & 0x0004) { // TOC field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700161 if (source->readAt(offset + 1, seeker->mTOC, 99) < 99) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800162 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700163 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800164 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700165 seeker->mTOCValid = true;
Andreas Huber4456da52010-11-09 08:57:45 -0800166 offset += 100;
167 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700168
169#if 0
Andreas Huber4456da52010-11-09 08:57:45 -0800170 if (flags & 0x0008) { // Quality indicator field is present
Marco Nelissen9e503852012-03-16 07:56:42 -0700171 if (source->readAt(offset, buffer, 4) < 4) {
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800172 delete seeker;
Marco Nelissen9e503852012-03-16 07:56:42 -0700173 return NULL;
Andreas Huber4456da52010-11-09 08:57:45 -0800174 }
Marco Nelissen9e503852012-03-16 07:56:42 -0700175 // do something with the quality indicator
176 offset += 4;
Andreas Huber4456da52010-11-09 08:57:45 -0800177 }
Marco Nelissenb636abd2012-03-19 13:49:43 -0700178
179 if (source->readAt(xingbase + 0xaf - 0x24, &buffer, 1) < 1) { // encoding flags
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800180 delete seeker;
Marco Nelissenb636abd2012-03-19 13:49:43 -0700181 return false;
182 }
183
184 ALOGV("nogap preceding: %s, nogap continued in next: %s",
185 (buffer[0] & 0x80) ? "true" : "false",
186 (buffer[0] & 0x40) ? "true" : "false");
Marco Nelissen9e503852012-03-16 07:56:42 -0700187#endif
188
Marco Nelissenb636abd2012-03-19 13:49:43 -0700189 if (source->readAt(xingbase + 0xb1 - 0x24, &buffer, 3) == 3) {
190 seeker->mEncoderDelay = (buffer[0] << 4) + (buffer[1] >> 4);
191 seeker->mEncoderPadding = ((buffer[1] & 0xf) << 8) + buffer[2];
192 }
193
Marco Nelissen9e503852012-03-16 07:56:42 -0700194 return seeker;
Andreas Huber4456da52010-11-09 08:57:45 -0800195}
196
Marco Nelissenb636abd2012-03-19 13:49:43 -0700197int32_t XINGSeeker::getEncoderDelay() {
198 return mEncoderDelay;
199}
200
201int32_t XINGSeeker::getEncoderPadding() {
202 return mEncoderPadding;
203}
204
Andreas Huber4456da52010-11-09 08:57:45 -0800205} // namespace android
206