summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/on2/dec/VPXDecoder.cpp
blob: 489e5ade7341ebf04707421ed6de45779af136ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "VPXDecoder"
#include <utils/Log.h>

#include "VPXDecoder.h"

#include <OMX_Component.h>

#include <media/stagefright/MediaBufferGroup.h>
#include <media/stagefright/MediaDebug.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/Utils.h>

#include "vpx/vpx_decoder.h"
#include "vpx/vpx_codec.h"
#include "vpx/vp8dx.h"

namespace android {

VPXDecoder::VPXDecoder(const sp<MediaSource> &source)
    : mSource(source),
      mStarted(false),
      mBufferSize(0),
      mCtx(NULL),
      mBufferGroup(NULL),
      mTargetTimeUs(-1) {
    sp<MetaData> inputFormat = source->getFormat();
    const char *mime;
    CHECK(inputFormat->findCString(kKeyMIMEType, &mime));
    CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_VPX));

    CHECK(inputFormat->findInt32(kKeyWidth, &mWidth));
    CHECK(inputFormat->findInt32(kKeyHeight, &mHeight));

    mBufferSize = (mWidth * mHeight * 3) / 2;

    mFormat = new MetaData;
    mFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
    mFormat->setInt32(kKeyWidth, mWidth);
    mFormat->setInt32(kKeyHeight, mHeight);
    mFormat->setInt32(kKeyColorFormat, OMX_COLOR_FormatYUV420Planar);
    mFormat->setCString(kKeyDecoderComponent, "VPXDecoder");

    int64_t durationUs;
    if (inputFormat->findInt64(kKeyDuration, &durationUs)) {
        mFormat->setInt64(kKeyDuration, durationUs);
    }
}

VPXDecoder::~VPXDecoder() {
    if (mStarted) {
        stop();
    }
}

status_t VPXDecoder::start(MetaData *) {
    if (mStarted) {
        return UNKNOWN_ERROR;
    }

    status_t err = mSource->start();

    if (err != OK) {
        return err;
    }

    mCtx = new vpx_codec_ctx_t;
    vpx_codec_err_t vpx_err;
    if ((vpx_err = vpx_codec_dec_init(
                (vpx_codec_ctx_t *)mCtx, &vpx_codec_vp8_dx_algo, NULL, 0))) {
        LOGE("on2 decoder failed to initialize. (%d)", vpx_err);

        mSource->stop();

        return UNKNOWN_ERROR;
    }

    mBufferGroup = new MediaBufferGroup;
    mBufferGroup->add_buffer(new MediaBuffer(mBufferSize));
    mBufferGroup->add_buffer(new MediaBuffer(mBufferSize));

    mTargetTimeUs = -1;

    mStarted = true;

    return OK;
}

status_t VPXDecoder::stop() {
    if (!mStarted) {
        return UNKNOWN_ERROR;
    }

    delete mBufferGroup;
    mBufferGroup = NULL;

    vpx_codec_destroy((vpx_codec_ctx_t *)mCtx);
    delete (vpx_codec_ctx_t *)mCtx;
    mCtx = NULL;

    mSource->stop();

    mStarted = false;

    return OK;
}

sp<MetaData> VPXDecoder::getFormat() {
    return mFormat;
}

status_t VPXDecoder::read(
        MediaBuffer **out, const ReadOptions *options) {
    *out = NULL;

    bool seeking = false;
    int64_t seekTimeUs;
    ReadOptions::SeekMode seekMode;
    if (options && options->getSeekTo(&seekTimeUs, &seekMode)) {
        seeking = true;
    }

    MediaBuffer *input;
    status_t err = mSource->read(&input, options);

    if (err != OK) {
        return err;
    }

    LOGV("read %d bytes from source\n", input->range_length());

    if (seeking) {
        int64_t targetTimeUs;
        if (input->meta_data()->findInt64(kKeyTargetTime, &targetTimeUs)
                && targetTimeUs >= 0) {
            mTargetTimeUs = targetTimeUs;
        } else {
            mTargetTimeUs = -1;
        }
    }

    if (vpx_codec_decode(
                (vpx_codec_ctx_t *)mCtx,
                (uint8_t *)input->data() + input->range_offset(),
                input->range_length(),
                NULL,
                0)) {
        LOGE("on2 decoder failed to decode frame.");
        input->release();
        input = NULL;

        return UNKNOWN_ERROR;
    }

    LOGV("successfully decoded 1 or more frames.");

    int64_t timeUs;
    CHECK(input->meta_data()->findInt64(kKeyTime, &timeUs));

    input->release();
    input = NULL;

    bool skipFrame = false;

    if (mTargetTimeUs >= 0) {
        CHECK(timeUs <= mTargetTimeUs);

        if (timeUs < mTargetTimeUs) {
            // We're still waiting for the frame with the matching
            // timestamp and we won't return the current one.
            skipFrame = true;

            LOGV("skipping frame at %lld us", timeUs);
        } else {
            LOGV("found target frame at %lld us", timeUs);

            mTargetTimeUs = -1;
        }
    }

    if (skipFrame) {
        *out = new MediaBuffer(0);
        return OK;
    }

    vpx_codec_iter_t iter = NULL;
    vpx_image_t *img = vpx_codec_get_frame((vpx_codec_ctx_t *)mCtx, &iter);

    if (img == NULL) {
        // The VPX format supports "internal-only" frames that are
        // referenced by future content but never actually displayed, so
        // this is a perfectly valid scenario.

        *out = new MediaBuffer(0);
        return OK;
    }

    CHECK_EQ(img->fmt, IMG_FMT_I420);

    int32_t width = img->d_w;
    int32_t height = img->d_h;

    if (width != mWidth || height != mHeight) {
        LOGI("Image dimensions changed, width = %d, height = %d",
             width, height);

        mWidth = width;
        mHeight = height;
        mFormat->setInt32(kKeyWidth, width);
        mFormat->setInt32(kKeyHeight, height);

        mBufferSize = (mWidth * mHeight * 3) / 2;
        delete mBufferGroup;
        mBufferGroup = new MediaBufferGroup;
        mBufferGroup->add_buffer(new MediaBuffer(mBufferSize));
        mBufferGroup->add_buffer(new MediaBuffer(mBufferSize));

        return INFO_FORMAT_CHANGED;
    }

    MediaBuffer *output;
    CHECK_EQ(mBufferGroup->acquire_buffer(&output), OK);

    const uint8_t *srcLine = (const uint8_t *)img->planes[PLANE_Y];
    uint8_t *dst = (uint8_t *)output->data();
    for (size_t i = 0; i < img->d_h; ++i) {
        memcpy(dst, srcLine, img->d_w);

        srcLine += img->stride[PLANE_Y];
        dst += img->d_w;
    }

    srcLine = (const uint8_t *)img->planes[PLANE_U];
    for (size_t i = 0; i < img->d_h / 2; ++i) {
        memcpy(dst, srcLine, img->d_w / 2);

        srcLine += img->stride[PLANE_U];
        dst += img->d_w / 2;
    }

    srcLine = (const uint8_t *)img->planes[PLANE_V];
    for (size_t i = 0; i < img->d_h / 2; ++i) {
        memcpy(dst, srcLine, img->d_w / 2);

        srcLine += img->stride[PLANE_V];
        dst += img->d_w / 2;
    }

    output->set_range(0, (width * height * 3) / 2);

    output->meta_data()->setInt64(kKeyTime, timeUs);

    *out = output;

    return OK;
}

}  // namespace android