File: v4l2_encode.cpp

package info (click to toggle)
libyami 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,152 kB
  • sloc: cpp: 44,247; ansic: 1,255; makefile: 728; lisp: 479; sh: 21; python: 19
file content (423 lines) | stat: -rw-r--r-- 16,376 bytes parent folder | download | duplicates (3)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Copyright (C) 2014 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <linux/videodev2.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>

#include "v4l2_encode.h"
#include "VideoEncoderHost.h"
#include "common/log.h"

V4l2Encoder::V4l2Encoder()
    : m_videoParamsChanged(false)
    , m_maxOutputBufferSize(0)
    , m_outputBufferSpace(NULL)
    , m_separatedStreamHeader(false)
    , m_requestStreamHeader(true)
    , m_forceKeyFrame(false)
{
    m_memoryMode[INPUT] = V4L2_MEMORY_USERPTR; // dma_buf hasn't been supported yet
    m_pixelFormat[INPUT] = V4L2_PIX_FMT_YUV420M;
    m_bufferPlaneCount[INPUT] = 3; // decided by m_pixelFormat[INPUT]
    m_memoryMode[OUTPUT] = V4L2_MEMORY_MMAP;
    m_pixelFormat[OUTPUT] = V4L2_PIX_FMT_H264;
    m_bufferPlaneCount[OUTPUT] = 1;
    m_maxBufferCount[INPUT] = 3;
    m_maxBufferCount[OUTPUT] = 3;

    m_inputFrames.resize(m_maxBufferCount[INPUT]);
    m_outputFrames.resize(m_maxBufferCount[OUTPUT]);
}

bool V4l2Encoder::start()
{
    YamiStatus status = YAMI_SUCCESS;
    ASSERT(m_encoder);

    VideoConfigAVCStreamFormat streamFormat;
    streamFormat.size = sizeof(VideoConfigAVCStreamFormat);
    streamFormat.streamFormat = AVC_STREAM_FORMAT_ANNEXB;
    m_encoder->setParameters(VideoConfigTypeAVCStreamFormat, &streamFormat);

    if (m_svct) {
        VideoTemporalLayers layers;
        layers.numLayersMinus1 = 1; // It is two layers actually
        layers.bitRate[0] = m_videoParams.rcParams.bitRate / 2;
        layers.bitRate[1] = m_videoParams.rcParams.bitRate;

        m_videoParams.temporalLayers = layers;
    }

    status = m_encoder->setParameters(VideoParamsTypeCommon, &m_videoParams);
    ASSERT(status == YAMI_SUCCESS);

    NativeDisplay nativeDisplay;
    nativeDisplay.type = NATIVE_DISPLAY_DRM;
    nativeDisplay.handle = 0;

    m_encoder->setNativeDisplay(&nativeDisplay);

    status = m_encoder->start();
    ASSERT(status == YAMI_SUCCESS);
    status = m_encoder->getParameters(VideoParamsTypeCommon, &m_videoParams);
    ASSERT(status == YAMI_SUCCESS);

    return true;
}

bool V4l2Encoder::stop()
{
    YamiStatus encodeStatus = YAMI_SUCCESS;
    if (m_encoder)
        encodeStatus = m_encoder->stop();
    return encodeStatus == YAMI_SUCCESS;
}


bool V4l2Encoder::UpdateVideoParameters(bool isInputThread)
{
    YamiStatus status = YAMI_SUCCESS;
    AutoLock locker(m_videoParamsLock); // make sure the caller has released m_videoParamsLock

    if (!m_videoParamsChanged)
        return true;

    if (isInputThread || !m_streamOn[INPUT]) {
        status = m_encoder->setParameters(VideoParamsTypeCommon, &m_videoParams);
        ASSERT(status == YAMI_SUCCESS);
        m_videoParamsChanged = false;
    }

    return status == YAMI_SUCCESS;
}
bool V4l2Encoder::inputPulse(uint32_t index)
{
    YamiStatus status = YAMI_SUCCESS;

    if(m_videoParamsChanged )
        UpdateVideoParameters(true);

    DEBUG_FOURCC("m_inputFrames[index].fourcc: ", m_inputFrames[index].fourcc);
    status = m_encoder->encode(&m_inputFrames[index]);
    ASSERT(m_inputFrames[index].bufAvailable); // check it at a later time when yami does encode in async

    if (status != YAMI_SUCCESS)
        return false;

    return true;
}

bool V4l2Encoder::outputPulse(uint32_t &index)
{
    YamiStatus status = YAMI_SUCCESS;

    VideoEncOutputBuffer *outputBuffer = &(m_outputFrames[index]);
    if (m_separatedStreamHeader) {
        outputBuffer->format = OUTPUT_FRAME_DATA;
        if (m_requestStreamHeader) {
            outputBuffer->format = OUTPUT_CODEC_DATA;
        }
    } else
        outputBuffer->format = OUTPUT_EVERYTHING;

    status = m_encoder->getOutput(outputBuffer, false);

    if (status != YAMI_SUCCESS)
        return false;

    ASSERT(m_maxOutputBufferSize > 0); // update m_maxOutputBufferSize after VIDIOC_S_FMT
    ASSERT(m_outputBufferSpace);
    ASSERT(outputBuffer->dataSize <= m_maxOutputBufferSize);

    if (m_separatedStreamHeader) {
        if (m_requestStreamHeader)
            m_requestStreamHeader = false;
    }

    return true;
}

bool V4l2Encoder::acceptInputBuffer(struct v4l2_buffer *qbuf)
{
    uint32_t i;
    VideoEncRawBuffer *inputBuffer = &(m_inputFrames[qbuf->index]);

    // XXX todo: add multiple planes support for yami
    inputBuffer->data = reinterpret_cast<uint8_t*>(qbuf->m.planes[0].m.userptr);
    inputBuffer->size = 0;
    for (i=0; i<qbuf->length; i++) {
       inputBuffer->size += qbuf->m.planes[i].bytesused;
    }
    inputBuffer->bufAvailable = false;
    DEBUG("qbuf->index: %d, inputBuffer: %p, bufAvailable: %d", qbuf->index, inputBuffer, inputBuffer->bufAvailable);
    inputBuffer->timeStamp = qbuf->timestamp.tv_sec * 1000000 + qbuf->timestamp.tv_usec; // XXX
    if (m_forceKeyFrame) {
        inputBuffer->forceKeyFrame = true;
        m_forceKeyFrame = false;
    }
    switch(m_pixelFormat[INPUT]) {
    case V4L2_PIX_FMT_NV12:
        inputBuffer->fourcc = VA_FOURCC_NV12;
        break;
    case V4L2_PIX_FMT_YUV420M:
        inputBuffer->fourcc = VA_FOURCC('I', '4', '2', '0');
        break;
    case V4L2_PIX_FMT_YUYV:
        inputBuffer->fourcc = VA_FOURCC_YUY2;
        break;
    default:
        ASSERT(0);
        break;
    }

    return true;
}

bool V4l2Encoder::giveOutputBuffer(struct v4l2_buffer *dqbuf)
{
    ASSERT(dqbuf->index < m_maxBufferCount[OUTPUT]);
    VideoEncOutputBuffer *outputBuffer = &(m_outputFrames[dqbuf->index]);
    dqbuf->m.planes[0].bytesused = outputBuffer->dataSize;
    dqbuf->bytesused = m_outputFrames[dqbuf->index].dataSize;
    dqbuf->m.planes[0].m.mem_offset = 0;
    ASSERT(m_maxOutputBufferSize > 0);
    ASSERT(m_outputBufferSpace);
    if (outputBuffer->flag & ENCODE_BUFFERFLAG_SYNCFRAME)
        dqbuf->flags = V4L2_BUF_FLAG_KEYFRAME;


    if (m_svct &&  (m_videoParams.temporalLayers.numLayersMinus1 > 0)
            && (outputBuffer->temporalID == m_videoParams.temporalLayers.numLayersMinus1)) {
        dqbuf->flags |= V4L2_BUF_FLAG_NON_REF;
    }

    return true;
}

int32_t V4l2Encoder::ioctl(int command, void* arg)
{
    YamiStatus encodeStatus = YAMI_SUCCESS;
    int32_t ret = 0;

    DEBUG("fd: %d, ioctl command: %s", m_fd[0], IoctlCommandString(command));
    switch (command) {
    case VIDIOC_QBUF:
    case VIDIOC_QUERYCAP:
    case VIDIOC_STREAMON:
    case VIDIOC_STREAMOFF:
    case VIDIOC_REQBUFS:
    case VIDIOC_DQBUF:
        ret = V4l2CodecBase::ioctl(command, arg);
        break;
    case VIDIOC_QUERYBUF: {
        struct v4l2_buffer *buffer = static_cast<struct v4l2_buffer*>(arg);
        ASSERT (buffer->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
        ASSERT(buffer->memory == V4L2_MEMORY_MMAP);
        ASSERT(buffer->index < m_maxBufferCount[OUTPUT]);
        ASSERT(buffer->length == m_bufferPlaneCount[OUTPUT]);
        ASSERT(m_maxOutputBufferSize > 0);

        buffer->m.planes[0].length = m_maxOutputBufferSize;
        buffer->m.planes[0].m.mem_offset = m_maxOutputBufferSize * buffer->index;
    }
    break;
    case VIDIOC_S_EXT_CTRLS: {
        uint32_t i;
        struct v4l2_ext_controls *control = static_cast<struct v4l2_ext_controls *>(arg);
        DEBUG("V4L2_CTRL_CLASS_MPEG: %d, control->ctrl_class: %d", V4L2_CTRL_CLASS_MPEG, control->ctrl_class);
        if (control->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
            AutoLock locker(m_videoParamsLock);
            struct v4l2_ext_control *ctrls = control->controls;
            DEBUG("control->count: %d", control->count);
            for (i=0; i<control->count; i++) {
                DEBUG("VIDIOC_S_EXT_CTRLS:V4L2_CTRL_CLASS_MPEG:%d", ctrls->id);
                switch (ctrls->id) {
                    case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
                        // ::EncodeTask
                        ASSERT(ctrls->value == V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_I_FRAME);
                        m_forceKeyFrame = true;
                        break;
                    case V4L2_CID_MPEG_VIDEO_BITRATE: {
                        // ::RequestEncodingParametersChangeTask
                        m_videoParams.rcParams.bitRate = ctrls->value;
                        m_videoParamsChanged = true;
                    }
                        break;
                    case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
                        INFO("enable bitrate control");
                        m_videoParams.rcMode = RATE_CONTROL_CBR;
                        m_videoParamsChanged = true;
                        break;
                    case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
                        // Separate stream header so we can cache it and insert into the stream.
                        if (ctrls->value == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)
                            m_separatedStreamHeader = true;
                        INFO("use separated stream header: %d", m_separatedStreamHeader);
                        break;
                    case V4L2_CID_MPEG_VIDEO_B_FRAMES:
                    case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
                    case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
                    case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
                    case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
                    case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
                    case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
                    default:
                        break;
                }
                ctrls++;
            }
        }
        UpdateVideoParameters();
    }
    break;
    case VIDIOC_S_PARM: {
        struct v4l2_streamparm *parms = static_cast<struct v4l2_streamparm *>(arg);
        if (parms->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
            AutoLock locker(m_videoParamsLock);
            // ::RequestEncodingParametersChangeTask
            m_videoParams.frameRate.frameRateNum = parms->parm.output.timeperframe.numerator;
            m_videoParams.frameRate.frameRateDenom = parms->parm.output.timeperframe.denominator;
            uint32_t framerate = m_videoParams.frameRate.frameRateNum/m_videoParams.frameRate.frameRateDenom;
            if (framerate * 2 < m_videoParams.intraPeriod) {
                m_videoParams.intraPeriod = framerate * 2;
            }
            m_videoParamsChanged = true;
        }
        UpdateVideoParameters();
    }
    break;
    case VIDIOC_S_FMT: {
        struct v4l2_format *format = static_cast<struct v4l2_format *>(arg);
        ASSERT(!m_streamOn[INPUT] && !m_streamOn[OUTPUT]);
        if (format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
            // ::SetOutputFormat
            switch (format->fmt.pix_mp.pixelformat) {
                case V4L2_PIX_FMT_H264: {
                    m_encoder.reset(createVideoEncoder(YAMI_MIME_H264), releaseVideoEncoder);
                    if (!m_encoder) {
                        ret = -1;
                        break;
                    }
                    m_videoParams.size = sizeof(m_videoParams);
                    encodeStatus = m_encoder->getParameters(VideoParamsTypeCommon, &m_videoParams);
                    ASSERT(encodeStatus == YAMI_SUCCESS);
                    m_videoParams.profile = VAProfileH264Main;
                    encodeStatus = m_encoder->setParameters(VideoParamsTypeCommon, &m_videoParams);
                    ASSERT(encodeStatus == YAMI_SUCCESS);
                }
                break;
                case V4L2_PIX_FMT_VP8:
                default:
                    ret = -1;
                break;
            }

        } else if (format->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
            // ::NegotiateInputFormat
            ASSERT(format->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_YUV420M
                || format->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_YUYV
                || format->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_NV12);
            m_pixelFormat[INPUT] = format->fmt.pix_mp.pixelformat;
            switch (m_pixelFormat[INPUT]) {
            case V4L2_PIX_FMT_YUV420M:
                m_bufferPlaneCount[INPUT] = 3;
                format->fmt.pix_mp.plane_fmt[0].bytesperline = m_videoParams.resolution.width;
                format->fmt.pix_mp.plane_fmt[0].sizeimage = m_videoParams.resolution.width * m_videoParams.resolution.height;
                format->fmt.pix_mp.plane_fmt[1].bytesperline = m_videoParams.resolution.width/2;
                format->fmt.pix_mp.plane_fmt[1].sizeimage = m_videoParams.resolution.width * m_videoParams.resolution.height /4;
                format->fmt.pix_mp.plane_fmt[2].bytesperline = m_videoParams.resolution.width/2;
                format->fmt.pix_mp.plane_fmt[2].sizeimage = m_videoParams.resolution.width * m_videoParams.resolution.height /4;
            break;
            case V4L2_PIX_FMT_NV12:
                m_bufferPlaneCount[INPUT] = 2;
                format->fmt.pix_mp.plane_fmt[0].bytesperline = m_videoParams.resolution.width;
                format->fmt.pix_mp.plane_fmt[0].sizeimage = m_videoParams.resolution.width * m_videoParams.resolution.height;
                format->fmt.pix_mp.plane_fmt[1].bytesperline = m_videoParams.resolution.width;
                format->fmt.pix_mp.plane_fmt[1].sizeimage = m_videoParams.resolution.width * m_videoParams.resolution.height /2;
            break;
            case V4L2_PIX_FMT_YUYV:
                m_bufferPlaneCount[INPUT] = 1;
                format->fmt.pix_mp.plane_fmt[0].bytesperline = m_videoParams.resolution.width * 2;
                format->fmt.pix_mp.plane_fmt[0].sizeimage = m_videoParams.resolution.width * m_videoParams.resolution.height * 2;
                break;
            default:
                ASSERT(0);
                break;
            }
            ASSERT(m_encoder);
            m_videoParams.resolution.width = format->fmt.pix_mp.width;
            m_videoParams.resolution.height= format->fmt.pix_mp.height;
            encodeStatus = m_encoder->setParameters(VideoParamsTypeCommon, &m_videoParams);
            ASSERT(encodeStatus == YAMI_SUCCESS);
            encodeStatus = m_encoder->getMaxOutSize(&m_maxOutputBufferSize);
            ASSERT(encodeStatus == YAMI_SUCCESS);
            INFO("resolution: %d x %d, m_maxOutputBufferSize: %d", m_videoParams.resolution.width,
                m_videoParams.resolution.height, m_maxOutputBufferSize);
        } else {
            ret = -1;
            ERROR("unknow type: %d of setting format VIDIOC_S_FMT", format->type);
        }
    }
    break;
    case VIDIOC_S_CROP: {
        // ::SetFormats
        //struct v4l2_crop *crop = static_cast<struct v4l2_crop *>(arg);
        INFO("ignore crop for now (the difference between buffer size and real size)");
    }
    break;
    default:
        ret = -1;
        ERROR("unknown ioctrl command: %d", command);
    break;
    }

    if (ret == -1 && errno != EAGAIN) {
        ERROR("ioctl failed");
        WARNING("ioctl command: %s failed", IoctlCommandString(command));
    }

    return ret;
}


void* V4l2Encoder::mmap (void* addr, size_t length,
                      int prot, int flags, unsigned int offset)
{
    uint32_t i;
    ASSERT((prot == PROT_READ) | PROT_WRITE);
    ASSERT(flags == MAP_SHARED);

    ASSERT(m_maxOutputBufferSize > 0);
    ASSERT(length <= m_maxOutputBufferSize);
    if (!m_outputBufferSpace) {
        m_outputBufferSpace = static_cast<uint8_t*>(malloc(m_maxOutputBufferSize * m_maxBufferCount[OUTPUT]));
        for (i=0; i<m_maxBufferCount[OUTPUT]; i++) {
            m_outputFrames[i].data = m_outputBufferSpace + m_maxOutputBufferSize*i;
            m_outputFrames[i].bufferSize = m_maxOutputBufferSize;
            m_outputFrames[i].format = OUTPUT_EVERYTHING;
        }
    }
    ASSERT(m_outputBufferSpace);

    return m_outputBufferSpace + offset;
}