1/*
2 * Copyright (C) 2012 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 SKIP_CUT_BUFFER_H_
18
19#define SKIP_CUT_BUFFER_H_
20
21#include <media/MediaCodecBuffer.h>
22#include <media/stagefright/MediaBuffer.h>
23#include <media/stagefright/foundation/ABuffer.h>
24
25namespace android {
26
27/**
28 * utility class to cut the start and end off a stream of data in MediaBuffers
29 *
30 */
31class SkipCutBuffer: public RefBase {
32 public:
33    // 'skip' is the number of frames to skip from the beginning
34    // 'cut' is the number of frames to cut from the end
35    // 'num16BitChannels' is the number of channels, which are assumed to be 16 bit wide each
36    SkipCutBuffer(size_t skip, size_t cut, size_t num16Channels);
37
38    // Submit one MediaBuffer for skipping and cutting. This may consume all or
39    // some of the data in the buffer, or it may add data to it.
40    // After this, the caller should continue processing the buffer as usual.
41    void submit(MediaBuffer *buffer);
42    void submit(const sp<ABuffer>& buffer);    // same as above, but with an ABuffer
43    void submit(const sp<MediaCodecBuffer>& buffer);    // same as above, but with an ABuffer
44    void clear();
45    size_t size(); // how many bytes are currently stored in the buffer
46
47 protected:
48    virtual ~SkipCutBuffer();
49
50 private:
51    void write(const char *src, size_t num);
52    size_t read(char *dst, size_t num);
53    template <typename T>
54    void submitInternal(const sp<T>& buffer);
55    int32_t mSkip;
56    int32_t mFrontPadding;
57    int32_t mBackPadding;
58    int32_t mWriteHead;
59    int32_t mReadHead;
60    int32_t mCapacity;
61    char* mCutBuffer;
62    DISALLOW_EVIL_CONSTRUCTORS(SkipCutBuffer);
63};
64
65}  // namespace android
66
67#endif  // OMX_CODEC_H_
68