1 /*
2  * Copyright (C) 2016 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 
17 #ifndef STAGEFRIGHT_DATACONVERTER_H_
18 #define STAGEFRIGHT_DATACONVERTER_H_
19 
20 #include <utils/Errors.h>
21 #include <utils/RefBase.h>
22 
23 #include <media/stagefright/MediaDefs.h>
24 
25 namespace android {
26 
27 class MediaCodecBuffer;
28 
29 // DataConverter base class, defaults to memcpy
30 struct DataConverter : public RefBase {
31     virtual size_t sourceSize(size_t targetSize); // will clamp to SIZE_MAX
32     virtual size_t targetSize(size_t sourceSize); // will clamp to SIZE_MAX
33 
34     status_t convert(const sp<MediaCodecBuffer> &source, sp<MediaCodecBuffer> &target);
35     virtual ~DataConverter();
36 
37 protected:
38     virtual status_t safeConvert(const sp<MediaCodecBuffer> &source, sp<MediaCodecBuffer> &target);
39 };
40 
41 // SampleConverterBase uses a ratio to calculate the source and target sizes
42 // based on source and target sample sizes.
43 struct SampleConverterBase : public DataConverter {
44     virtual size_t sourceSize(size_t targetSize);
45     virtual size_t targetSize(size_t sourceSize);
46 
47 protected:
48     virtual status_t safeConvert(const sp<MediaCodecBuffer> &source, sp<MediaCodecBuffer> &target) = 0;
49 
50     // sourceSize = sourceSampleSize / targetSampleSize * targetSize
SampleConverterBaseSampleConverterBase51     SampleConverterBase(uint32_t sourceSampleSize, uint32_t targetSampleSize)
52         : mSourceSampleSize(sourceSampleSize),
53           mTargetSampleSize(targetSampleSize) { }
54     size_t mSourceSampleSize;
55     size_t mTargetSampleSize;
56 };
57 
58 // AudioConverter converts between audio PCM formats
59 struct AudioConverter : public SampleConverterBase {
60     // return nullptr if conversion is not needed or not supported
61     static AudioConverter *Create(AudioEncoding source, AudioEncoding target);
62 
63 protected:
64     virtual status_t safeConvert(const sp<MediaCodecBuffer> &source, sp<MediaCodecBuffer> &target);
65 
66 private:
AudioConverterAudioConverter67     AudioConverter(
68             AudioEncoding source, size_t sourceSample,
69             AudioEncoding target, size_t targetSample)
70         : SampleConverterBase(sourceSample, targetSample),
71           mFrom(source),
72           mTo(target) { }
73     AudioEncoding mFrom;
74     AudioEncoding mTo;
75 };
76 
77 } // namespace android
78 
79 #endif
80