summaryrefslogtreecommitdiffstats
path: root/videoencoder/VideoEncoderMP4.cpp
blob: f6921633cad0a1d74a7eab3fb64dbe1b1b2ff409 (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
278
279
280
281
/*
* Copyright (c) 2009-2011 Intel Corporation.  All rights reserved.
*
* 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.
*/

#include <string.h>
#include <stdlib.h>

#include "VideoEncoderLog.h"
#include "VideoEncoderMP4.h"
#include <va/va_tpi.h>

VideoEncoderMP4::VideoEncoderMP4()
    :mProfileLevelIndication(3)
    ,mFixedVOPTimeIncrement(0) {
    mComParams.profile = (VAProfile)PROFILE_MPEG4SIMPLE;
    mAutoReferenceSurfaceNum = 2;
}

Encode_Status VideoEncoderMP4::getHeaderPos(
        uint8_t *inBuffer, uint32_t bufSize, uint32_t *headerSize) {

    uint32_t bytesLeft = bufSize;

    *headerSize = 0;
    CHECK_NULL_RETURN_IFFAIL(inBuffer);

    if (bufSize < 4) {
        //bufSize shoule not < 4
        LOG_E("Buffer size too small\n");
        return ENCODE_FAIL;
    }

    while (bytesLeft > 4  &&
            (memcmp("\x00\x00\x01\xB6", &inBuffer[bufSize - bytesLeft], 4) &&
             memcmp("\x00\x00\x01\xB3", &inBuffer[bufSize - bytesLeft], 4))) {
        --bytesLeft;
    }

    if (bytesLeft <= 4) {
        LOG_E("NO header found\n");
        *headerSize = 0; //
    } else {
        *headerSize = bufSize - bytesLeft;
    }

    return ENCODE_SUCCESS;
}

Encode_Status VideoEncoderMP4::outputConfigData(
        VideoEncOutputBuffer *outBuffer) {

    Encode_Status ret = ENCODE_SUCCESS;
    uint32_t headerSize = 0;

    ret = getHeaderPos((uint8_t *)mCurSegment->buf + mOffsetInSeg,
            mCurSegment->size - mOffsetInSeg, &headerSize);
    CHECK_ENCODE_STATUS_RETURN("getHeaderPos");
    if (headerSize == 0) {
        outBuffer->dataSize = 0;
        mCurSegment = NULL;
        return ENCODE_NO_REQUEST_DATA;
    }

    if (headerSize <= outBuffer->bufferSize) {
        memcpy(outBuffer->data, (uint8_t *)mCurSegment->buf + mOffsetInSeg, headerSize);
        mTotalSizeCopied += headerSize;
        mOffsetInSeg += headerSize;
        outBuffer->dataSize = headerSize;
        outBuffer->remainingSize = 0;
        outBuffer->flag |= ENCODE_BUFFERFLAG_ENDOFFRAME;
        outBuffer->flag |= ENCODE_BUFFERFLAG_CODECCONFIG;
        outBuffer->flag |= ENCODE_BUFFERFLAG_SYNCFRAME;
    } else {
        // we need a big enough buffer, otherwise we won't output anything
        outBuffer->dataSize = 0;
        outBuffer->remainingSize = headerSize;
        outBuffer->flag |= ENCODE_BUFFERFLAG_DATAINVALID;
        LOG_E("Buffer size too small\n");
        return ENCODE_BUFFER_TOO_SMALL;
    }

    return ret;
}

Encode_Status VideoEncoderMP4::getExtFormatOutput(VideoEncOutputBuffer *outBuffer) {

    Encode_Status ret = ENCODE_SUCCESS;

    LOG_V("Begin\n");
    CHECK_NULL_RETURN_IFFAIL(outBuffer);

    switch (outBuffer->format) {
        case OUTPUT_CODEC_DATA: {
            // Output the codec config data
            ret = outputConfigData(outBuffer);
            CHECK_ENCODE_STATUS_CLEANUP("outputCodecData");
            break;
        }
        default:
            LOG_E("Invalid buffer mode for MPEG-4:2\n");
            ret = ENCODE_FAIL;
            break;
    }

    LOG_I("out size is = %d\n", outBuffer->dataSize);


CLEAN_UP:

    LOG_V("End\n");
    return ret;
}

Encode_Status VideoEncoderMP4::renderSequenceParams(EncodeTask *) {

    VAStatus vaStatus = VA_STATUS_SUCCESS;
    VAEncSequenceParameterBufferMPEG4 mp4SequenceParams = VAEncSequenceParameterBufferMPEG4();

    uint32_t frameRateNum = mComParams.frameRate.frameRateNum;
    uint32_t frameRateDenom = mComParams.frameRate.frameRateDenom;

    LOG_V( "Begin\n\n");
    // set up the sequence params for HW
    mp4SequenceParams.profile_and_level_indication = mProfileLevelIndication;
    mp4SequenceParams.video_object_layer_width= mComParams.resolution.width;
    mp4SequenceParams.video_object_layer_height= mComParams.resolution.height;
    mp4SequenceParams.vop_time_increment_resolution =
            (unsigned int) (frameRateNum + frameRateDenom /2) / frameRateDenom;
    mp4SequenceParams.fixed_vop_time_increment= mFixedVOPTimeIncrement;
    mp4SequenceParams.bits_per_second= mComParams.rcParams.bitRate;
    mp4SequenceParams.frame_rate =
            (unsigned int) (frameRateNum + frameRateDenom /2) / frameRateDenom;
    mp4SequenceParams.initial_qp = mComParams.rcParams.initQP;
    mp4SequenceParams.min_qp = mComParams.rcParams.minQP;
    mp4SequenceParams.intra_period = mComParams.intraPeriod;
    //mpeg4_seq_param.fixed_vop_rate = 30;

    LOG_V("===mpeg4 sequence params===\n");
    LOG_I("profile_and_level_indication = %d\n", (uint32_t)mp4SequenceParams.profile_and_level_indication);
    LOG_I("intra_period = %d\n", mp4SequenceParams.intra_period);
    LOG_I("video_object_layer_width = %d\n", mp4SequenceParams.video_object_layer_width);
    LOG_I("video_object_layer_height = %d\n", mp4SequenceParams.video_object_layer_height);
    LOG_I("vop_time_increment_resolution = %d\n", mp4SequenceParams.vop_time_increment_resolution);
    LOG_I("fixed_vop_rate = %d\n", mp4SequenceParams.fixed_vop_rate);
    LOG_I("fixed_vop_time_increment = %d\n", mp4SequenceParams.fixed_vop_time_increment);
    LOG_I("bitrate = %d\n", mp4SequenceParams.bits_per_second);
    LOG_I("frame_rate = %d\n", mp4SequenceParams.frame_rate);
    LOG_I("initial_qp = %d\n", mp4SequenceParams.initial_qp);
    LOG_I("min_qp = %d\n", mp4SequenceParams.min_qp);
    LOG_I("intra_period = %d\n\n", mp4SequenceParams.intra_period);

    vaStatus = vaCreateBuffer(
            mVADisplay, mVAContext,
            VAEncSequenceParameterBufferType,
            sizeof(mp4SequenceParams),
            1, &mp4SequenceParams,
            &mSeqParamBuf);
    CHECK_VA_STATUS_RETURN("vaCreateBuffer");

    vaStatus = vaRenderPicture(mVADisplay, mVAContext, &mSeqParamBuf, 1);
    CHECK_VA_STATUS_RETURN("vaRenderPicture");

    LOG_V( "end\n");
    return ENCODE_SUCCESS;
}

Encode_Status VideoEncoderMP4::renderPictureParams(EncodeTask *task) {

    VAStatus vaStatus = VA_STATUS_SUCCESS;
    VAEncPictureParameterBufferMPEG4 mpeg4_pic_param = VAEncPictureParameterBufferMPEG4();
    LOG_V( "Begin\n\n");
    // set picture params for HW
    if(mAutoReference == false){
        mpeg4_pic_param.reference_picture = task->ref_surface;
        mpeg4_pic_param.reconstructed_picture = task->rec_surface;
    }else {
        mpeg4_pic_param.reference_picture = mAutoRefSurfaces[0];
        mpeg4_pic_param.reconstructed_picture = mAutoRefSurfaces[1];
    }

    mpeg4_pic_param.coded_buf = task->coded_buffer;
    mpeg4_pic_param.picture_width = mComParams.resolution.width;
    mpeg4_pic_param.picture_height = mComParams.resolution.height;
    mpeg4_pic_param.vop_time_increment= mFrameNum;
    mpeg4_pic_param.picture_type = (task->type == FTYPE_I) ? VAEncPictureTypeIntra : VAEncPictureTypePredictive;

    LOG_V("======mpeg4 picture params======\n");
    LOG_V("reference_picture = 0x%08x\n", mpeg4_pic_param.reference_picture);
    LOG_V("reconstructed_picture = 0x%08x\n", mpeg4_pic_param.reconstructed_picture);
    LOG_V("coded_buf = 0x%08x\n", mpeg4_pic_param.coded_buf);
//    LOG_I("coded_buf_index = %d\n", mCodedBufIndex);
    LOG_V("picture_width = %d\n", mpeg4_pic_param.picture_width);
    LOG_V("picture_height = %d\n", mpeg4_pic_param.picture_height);
    LOG_V("vop_time_increment = %d\n", mpeg4_pic_param.vop_time_increment);
    LOG_V("picture_type = %d\n\n", mpeg4_pic_param.picture_type);

    vaStatus = vaCreateBuffer(
            mVADisplay, mVAContext,
            VAEncPictureParameterBufferType,
            sizeof(mpeg4_pic_param),
            1,&mpeg4_pic_param,
            &mPicParamBuf);
    CHECK_VA_STATUS_RETURN("vaCreateBuffer");

    vaStatus = vaRenderPicture(mVADisplay, mVAContext, &mPicParamBuf, 1);
    CHECK_VA_STATUS_RETURN("vaRenderPicture");

    return ENCODE_SUCCESS;
}


Encode_Status VideoEncoderMP4::renderSliceParams(EncodeTask *task) {

    VAStatus vaStatus = VA_STATUS_SUCCESS;
    uint32_t sliceHeight;
    uint32_t sliceHeightInMB;

    VAEncSliceParameterBuffer sliceParams;

    LOG_V( "Begin\n\n");

    sliceHeight = mComParams.resolution.height;
    sliceHeight += 15;
    sliceHeight &= (~15);
    sliceHeightInMB = sliceHeight / 16;

    sliceParams.start_row_number = 0;
    sliceParams.slice_height = sliceHeightInMB;
    sliceParams.slice_flags.bits.is_intra = (task->type == FTYPE_I)?1:0;
    sliceParams.slice_flags.bits.disable_deblocking_filter_idc = 0;

    LOG_V("======mpeg4 slice params======\n");
    LOG_I( "start_row_number = %d\n", (int) sliceParams.start_row_number);
    LOG_I( "sliceHeightInMB = %d\n", (int) sliceParams.slice_height);
    LOG_I( "is_intra = %d\n", (int) sliceParams.slice_flags.bits.is_intra);

    vaStatus = vaCreateBuffer(
            mVADisplay, mVAContext,
            VAEncSliceParameterBufferType,
            sizeof(VAEncSliceParameterBuffer),
            1, &sliceParams,
            &mSliceParamBuf);
    CHECK_VA_STATUS_RETURN("vaCreateBuffer");

    vaStatus = vaRenderPicture(mVADisplay, mVAContext, &mSliceParamBuf, 1);
    CHECK_VA_STATUS_RETURN("vaRenderPicture");

    LOG_V( "end\n");
    return ENCODE_SUCCESS;
}

Encode_Status VideoEncoderMP4::sendEncodeCommand(EncodeTask *task) {
    Encode_Status ret = ENCODE_SUCCESS;
    LOG_V( "Begin\n");

    if (mFrameNum == 0) {
        ret = renderSequenceParams(task);
        CHECK_ENCODE_STATUS_RETURN("renderSequenceParams");
    }

    ret = renderPictureParams(task);
    CHECK_ENCODE_STATUS_RETURN("renderPictureParams");

    ret = renderSliceParams(task);
    CHECK_ENCODE_STATUS_RETURN("renderPictureParams");

    LOG_V( "End\n");
    return ENCODE_SUCCESS;
}