File size: 24,582 Bytes
dffe123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html

#include "precomp.hpp"

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <android/log.h>
#include <android/native_window.h>
#include "media/NdkMediaCodec.h"
#include "media/NdkMediaMuxer.h"
#include "media/NdkMediaExtractor.h"
#include "media/NdkMediaFormat.h"

#define INPUT_TIMEOUT_MS 2000

#define COLOR_FormatYUV420Planar 19
#define COLOR_FormatYUV420SemiPlanar 21
#define COLOR_FormatSurface 0x7f000789 //See https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities for codes

using namespace cv;

#define TAG "NativeCodec"
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)


static inline void deleter_AMediaExtractor(AMediaExtractor *extractor) {
    AMediaExtractor_delete(extractor);
}

static inline void deleter_AMediaCodec(AMediaCodec *codec) {
    AMediaCodec_stop(codec);
    AMediaCodec_delete(codec);
}

static inline void deleter_AMediaFormat(AMediaFormat *format) {
    AMediaFormat_delete(format);
}

class AndroidMediaNdkCapture : public IVideoCapture
{

public:
    AndroidMediaNdkCapture():
        sawInputEOS(false), sawOutputEOS(false),
        frameStride(0), frameWidth(0), frameHeight(0), colorFormat(0),
        videoWidth(0), videoHeight(0),
        videoFrameCount(0),
        videoRotation(0), videoRotationCode(-1),
        videoOrientationAuto(false) {}

    std::shared_ptr<AMediaExtractor> mediaExtractor;
    std::shared_ptr<AMediaCodec> mediaCodec;
    bool sawInputEOS;
    bool sawOutputEOS;
    int32_t frameStride;
    int32_t frameWidth;
    int32_t frameHeight;
    int32_t colorFormat;
    int32_t videoWidth;
    int32_t videoHeight;
    float videoFrameRate;
    int32_t videoFrameCount;
    int32_t videoRotation;
    int32_t videoRotationCode;
    bool videoOrientationAuto;
    std::vector<uint8_t> buffer;
    Mat frame;

    ~AndroidMediaNdkCapture() { cleanUp(); }

    bool decodeFrame() {
        while (!sawInputEOS || !sawOutputEOS) {
            if (!sawInputEOS) {
                auto bufferIndex = AMediaCodec_dequeueInputBuffer(mediaCodec.get(), INPUT_TIMEOUT_MS);
                LOGV("input buffer %zd", bufferIndex);
                if (bufferIndex >= 0) {
                    size_t bufferSize;
                    auto inputBuffer = AMediaCodec_getInputBuffer(mediaCodec.get(), bufferIndex, &bufferSize);
                    auto sampleSize = AMediaExtractor_readSampleData(mediaExtractor.get(), inputBuffer, bufferSize);
                    if (sampleSize < 0) {
                        sampleSize = 0;
                        sawInputEOS = true;
                        LOGV("EOS");
                    }
                    auto presentationTimeUs = AMediaExtractor_getSampleTime(mediaExtractor.get());

                    AMediaCodec_queueInputBuffer(mediaCodec.get(), bufferIndex, 0, sampleSize,
                        presentationTimeUs, sawInputEOS ? AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM : 0);
                    AMediaExtractor_advance(mediaExtractor.get());
                }
            }

            if (!sawOutputEOS) {
                AMediaCodecBufferInfo info;
                auto bufferIndex = AMediaCodec_dequeueOutputBuffer(mediaCodec.get(), &info, 0);
                if (bufferIndex >= 0) {
                    size_t bufferSize = 0;
                    auto mediaFormat = std::shared_ptr<AMediaFormat>(AMediaCodec_getOutputFormat(mediaCodec.get()), deleter_AMediaFormat);
                    AMediaFormat_getInt32(mediaFormat.get(), AMEDIAFORMAT_KEY_WIDTH, &frameWidth);
                    AMediaFormat_getInt32(mediaFormat.get(), AMEDIAFORMAT_KEY_STRIDE, &frameStride);
                    AMediaFormat_getInt32(mediaFormat.get(), AMEDIAFORMAT_KEY_HEIGHT, &frameHeight);
                    AMediaFormat_getInt32(mediaFormat.get(), AMEDIAFORMAT_KEY_COLOR_FORMAT, &colorFormat);
                    uint8_t* codecBuffer = AMediaCodec_getOutputBuffer(mediaCodec.get(), bufferIndex, &bufferSize);
                    buffer = std::vector<uint8_t>(codecBuffer, codecBuffer + bufferSize);
                    LOGV("colorFormat: %d", colorFormat);
                    LOGV("buffer size: %zu", bufferSize);
                    LOGV("width (frame): %d", frameWidth);
                    LOGV("stride (frame): %d", frameStride);
                    LOGV("height (frame): %d", frameHeight);
                    if (info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)
                    {
                        LOGV("output EOS");
                        sawOutputEOS = true;
                    }

                    AMediaCodec_releaseOutputBuffer(mediaCodec.get(), bufferIndex, info.size != 0);
                    return true;
                } else if (bufferIndex == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
                    LOGV("output buffers changed");
                } else if (bufferIndex == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
                    auto format = AMediaCodec_getOutputFormat(mediaCodec.get());
                    LOGV("format changed to: %s", AMediaFormat_toString(format));
                    AMediaFormat_delete(format);
                } else if (bufferIndex == AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
                    LOGV("no output buffer right now");
                } else {
                    LOGV("unexpected info code: %zd", bufferIndex);
                }
            }
        }
        return false;
    }

    bool isOpened() const CV_OVERRIDE { return mediaCodec.get() != nullptr; }

    int getCaptureDomain() CV_OVERRIDE { return CAP_ANDROID; }

    bool grabFrame() CV_OVERRIDE
    {
        // clear the previous frame
        buffer.clear();
        return decodeFrame();
    }

    bool retrieveFrame(int, OutputArray out) CV_OVERRIDE
    {
        if (buffer.empty()) {
            return false;
        }

        Mat yuv(frameHeight + frameHeight/2, frameStride, CV_8UC1, buffer.data());

        if (colorFormat == COLOR_FormatYUV420Planar) {
            cv::cvtColor(yuv, frame, cv::COLOR_YUV2BGR_YV12);
        } else if (colorFormat == COLOR_FormatYUV420SemiPlanar) {
            cv::cvtColor(yuv, frame, cv::COLOR_YUV2BGR_NV21);
        } else {
            LOGE("Unsupported video format: %d", colorFormat);
            return false;
        }

        Mat croppedFrame = frame(Rect(0, 0, videoWidth, videoHeight));
        out.assign(croppedFrame);

        if (videoOrientationAuto && -1 != videoRotationCode) {
            cv::rotate(out, out, videoRotationCode);
        }

        return true;
    }

    double getProperty(int property_id) const CV_OVERRIDE
    {
        switch (property_id)
        {
            case CV_CAP_PROP_FRAME_WIDTH:
                return (( videoOrientationAuto &&
                         (cv::ROTATE_90_CLOCKWISE == videoRotationCode || cv::ROTATE_90_COUNTERCLOCKWISE == videoRotationCode))
                        ? videoHeight : videoWidth);
            case CV_CAP_PROP_FRAME_HEIGHT:
                return (( videoOrientationAuto &&
                         (cv::ROTATE_90_CLOCKWISE == videoRotationCode || cv::ROTATE_90_COUNTERCLOCKWISE == videoRotationCode))
                        ? videoWidth : videoHeight);
            case CV_CAP_PROP_FPS: return videoFrameRate;
            case CV_CAP_PROP_FRAME_COUNT: return videoFrameCount;
            case CAP_PROP_ORIENTATION_META: return videoRotation;
            case CAP_PROP_ORIENTATION_AUTO: return videoOrientationAuto ? 1 : 0;
        }
        return 0;
    }

    bool setProperty(int property_id, double value) CV_OVERRIDE
    {
        switch (property_id)
        {
            case CAP_PROP_ORIENTATION_AUTO: {
                videoOrientationAuto = value != 0 ? true : false;
                return true;
            }
        }

        return false;
    }

    bool initCapture(const char * filename)
    {
        struct stat statBuffer;
        if (stat(filename, &statBuffer) != 0) {
            LOGE("failed to stat file: %s (%s)", filename, strerror(errno));
            return false;
        }

        int fd = open(filename, O_RDONLY);

        if (fd < 0) {
            LOGE("failed to open file: %s %d (%s)", filename, fd, strerror(errno));
            return false;
        }

        mediaExtractor = std::shared_ptr<AMediaExtractor>(AMediaExtractor_new(), deleter_AMediaExtractor);
        if (!mediaExtractor) {
            return false;
        }
        media_status_t err = AMediaExtractor_setDataSourceFd(mediaExtractor.get(), fd, 0, statBuffer.st_size);
        close(fd);
        if (err != AMEDIA_OK) {
            LOGV("setDataSource error: %d", err);
            return false;
        }

        int numtracks = AMediaExtractor_getTrackCount(mediaExtractor.get());

        LOGV("input has %d tracks", numtracks);
        for (int i = 0; i < numtracks; i++) {
            auto format = std::shared_ptr<AMediaFormat>(AMediaExtractor_getTrackFormat(mediaExtractor.get(), i), deleter_AMediaFormat);
            if (!format) {
                continue;
            }
            const char *s = AMediaFormat_toString(format.get());
            LOGV("track %d format: %s", i, s);
            const char *mime;
            if (!AMediaFormat_getString(format.get(), AMEDIAFORMAT_KEY_MIME, &mime)) {
                LOGV("no mime type");
            } else if (!strncmp(mime, "video/", 6)) {
                int32_t trackWidth, trackHeight, fps, frameCount = 0, rotation = 0;
                AMediaFormat_getInt32(format.get(), AMEDIAFORMAT_KEY_WIDTH, &trackWidth);
                AMediaFormat_getInt32(format.get(), AMEDIAFORMAT_KEY_HEIGHT, &trackHeight);
                AMediaFormat_getInt32(format.get(), AMEDIAFORMAT_KEY_FRAME_RATE, &fps);

                #if __ANDROID_API__ >= 28
                    AMediaFormat_getInt32(format.get(), AMEDIAFORMAT_KEY_ROTATION, &rotation);
                    LOGV("rotation (track): %d", rotation);
                #endif

                #if __ANDROID_API__ >= 29
                    AMediaFormat_getInt32(format.get(), AMEDIAFORMAT_KEY_FRAME_COUNT, &frameCount);
                #endif

                LOGV("width (track): %d", trackWidth);
                LOGV("height (track): %d", trackHeight);
                if (AMediaExtractor_selectTrack(mediaExtractor.get(), i) != AMEDIA_OK) {
                    continue;
                }
                mediaCodec = std::shared_ptr<AMediaCodec>(AMediaCodec_createDecoderByType(mime), deleter_AMediaCodec);
                if (!mediaCodec) {
                    continue;
                }
                if (AMediaCodec_configure(mediaCodec.get(), format.get(), NULL, NULL, 0) != AMEDIA_OK) {
                    continue;
                }
                sawInputEOS = false;
                sawOutputEOS = false;
                if (AMediaCodec_start(mediaCodec.get()) != AMEDIA_OK) {
                    continue;
                }

                videoWidth = trackWidth;
                videoHeight = trackHeight;
                videoFrameRate = fps;
                videoFrameCount = frameCount;
                videoRotation = rotation;

                switch(videoRotation) {
                    case 90:
                        videoRotationCode = cv::ROTATE_90_CLOCKWISE;
                        break;

                    case 180:
                        videoRotationCode = cv::ROTATE_180;
                        break;

                    case 270:
                        videoRotationCode = cv::ROTATE_90_COUNTERCLOCKWISE;
                        break;

                    default:
                        videoRotationCode = -1;
                        break;
                }

                return true;
            }
        }

        return false;
    }

    void cleanUp() {
        sawInputEOS = true;
        sawOutputEOS = true;
        frameStride = 0;
        frameWidth = 0;
        frameHeight = 0;
        colorFormat = 0;
        videoWidth = 0;
        videoHeight = 0;
        videoFrameRate = 0;
        videoFrameCount = 0;
        videoRotation = 0;
        videoRotationCode = -1;
    }
};



class AndroidMediaNdkVideoWriter CV_FINAL :
    public cv::IVideoWriter
{
    typedef struct {
        int fourcc;
        const char* mime;
        OutputFormat muxerFormat;
    }
    FourCCInfo;

    static const int64_t TIMEOUT = 1000L;
    static const FourCCInfo FOURCC_INFO[];

    static const FourCCInfo* findInfo(int fourcc) {
        for( const FourCCInfo *it = FOURCC_INFO; NULL != it->mime; it++ ) {
            if (fourcc == it->fourcc) return it;
        }
        return NULL;
    }

    AMediaFormat* format;
    AMediaCodec* encoder;
    AMediaMuxer* muxer;

    #if __ANDROID_API__ >= 26
    ANativeWindow* surface;
    #endif

    long frameIndex;
    int width;
    int height;
    double frameRate;
    ssize_t videoTrackIndex;
    int fd;

    void drainEncoder(bool end) {
        if (end) {
            #if __ANDROID_API__ >= 26
            AMediaCodec_signalEndOfInputStream(encoder);
            #else
            writeBytes(NULL, 0);
            #endif
        }

        AMediaCodecBufferInfo bufferInfo;
        ssize_t bufferIndex;
        size_t  bufferSize;
        uint8_t *buffer;

        while (true) {
            bufferIndex = AMediaCodec_dequeueOutputBuffer(encoder, &bufferInfo, TIMEOUT);
            if (bufferIndex >= 0) {
                buffer = AMediaCodec_getOutputBuffer(encoder, (size_t)bufferIndex, &bufferSize);

                if (NULL == buffer || 0 == bufferSize){
                    LOGE("Can't get output buffer");
                    break;
                }

                if (videoTrackIndex >= 0) {
                    bufferInfo.presentationTimeUs = frameIndex * 1000000L / frameRate;
                    LOGV("Muxer write to track %d: %d byte(s)", (int)videoTrackIndex, (int)bufferInfo.size);
                    AMediaMuxer_writeSampleData(muxer, (size_t)videoTrackIndex, buffer, &bufferInfo);
                } else {
                    LOGE("Invalid video track !");
                }

                AMediaCodec_releaseOutputBuffer(encoder, (size_t)bufferIndex, false);
                if (bufferInfo.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) break;
            } else if (AMEDIACODEC_INFO_TRY_AGAIN_LATER == bufferIndex) {
                if (!end) break;
            } else if (AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED == bufferIndex) {
                videoTrackIndex = AMediaMuxer_addTrack(muxer, AMediaCodec_getOutputFormat(encoder));
                if (videoTrackIndex >= 0) {
                    AMediaMuxer_start(muxer);
                }
                LOGV("New videoTrackIndex: %d", (int)videoTrackIndex);
            }
        }
    }

    #if __ANDROID_API__ < 26
    void writeBytes( uint8_t* inputBuffer, size_t inputBufferSize ) {
        LOGV("[writeBytes] inputBufferSize=%u", (unsigned int)inputBufferSize);

        ssize_t bufferIndex;
        size_t  bufferSize;
        uint8_t* buffer;
        size_t  partialSize;
        bool firstCall = true;
        uint32_t flags;

        while(inputBufferSize > 0 || firstCall) {
            bufferIndex = AMediaCodec_dequeueInputBuffer(encoder, TIMEOUT);

            if (bufferIndex >= 0) {
                firstCall = false;
                buffer = AMediaCodec_getInputBuffer(encoder, (size_t)bufferIndex, &bufferSize);
                if (NULL == buffer || 0 == bufferSize) break;

                flags = 0;
                partialSize = (inputBufferSize > bufferSize) ? bufferSize : inputBufferSize;
                if (partialSize > 0) {
                    memcpy(buffer, inputBuffer, partialSize);
                    inputBuffer += partialSize;
                    inputBufferSize -= partialSize;
                    if (inputBufferSize > 0) {
                        flags = AMEDIACODEC_BUFFER_FLAG_PARTIAL_FRAME;
                    }
                } else {
                    flags = AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM;
                }

                LOGV(
                    "[writeBytes] partial - bufferIndex=%d, bufferSize=%u, partialSize=%u, remaining inputBufferSize=%u",
                    (int)bufferIndex, (unsigned int)bufferSize, (unsigned int)partialSize, (unsigned int)inputBufferSize
                );

                AMediaCodec_queueInputBuffer(encoder, (size_t)bufferIndex, 0, partialSize, frameIndex * 1000000L / frameRate, flags);
                if (NULL != inputBuffer) drainEncoder(false);
            }
        }
    }
    #endif

public:
    AndroidMediaNdkVideoWriter(const cv::String& filename, int fourcc, double fps, cv::Size frameSize, const VideoWriterParameters& params)
        : format(NULL),
          encoder(NULL),
          muxer(NULL),
          #if __ANDROID_API__ >= 26
          surface(NULL),
          #endif
          frameIndex(0),
          width(0),
          height(0),
          frameRate(0.),
          videoTrackIndex(-1),
          fd(-1) {
        open(filename, fourcc, fps, frameSize, params);
    }
    virtual ~AndroidMediaNdkVideoWriter() { close(); }

    virtual int getCaptureDomain() const CV_OVERRIDE { return cv::CAP_ANDROID; }

    virtual void write(cv::InputArray image_ ) CV_OVERRIDE
    {
        if (!image_.isMat()) {
            LOGE("Support only Mat input");
            return;
        }

        Mat image = image_.getMat();
        if (CV_8UC3 != image.type() || image.cols > width || image.rows > height) {
            LOGE(
                "Expected input to be a mat of maximum %d x %d of type CV_8UC3 (%d), but received %d x %d of type: %d",
                width, height, CV_8UC3,
                image.cols, image.rows, image.type()
            );
            return;
        }

        #if __ANDROID_API__ >= 26
        ANativeWindow_Buffer buffer;
        if (0 != ANativeWindow_lock(surface, &buffer, NULL)) {
            LOGE("Failed to lock the surface");
        } else {
            if (AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM == buffer.format) {
                Mat bufferMat(image.rows, image.cols, CV_8UC4, buffer.bits, buffer.stride * 4);
                cvtColor(image, bufferMat, CV_BGR2RGBA);
            } else {
                LOGE("Unknow surface buffer format: %u", buffer.format);
            }

            ANativeWindow_unlockAndPost(surface);
        }
        #else
        LOGV("[write] image: %d  x %d", image.cols, image.rows);

        //OpenCV don't support RGB to NV12 so we need to connvert to YV12 and then manually changed it to NV12
        Mat imageYV12;
        cvtColor(image, imageYV12, CV_BGR2YUV_YV12);

        //convert from YV12 to NV12
        size_t yPlaneSize = width * height;
        size_t vPlaneSize = yPlaneSize / 4;

        Mat channels[2] = {
            Mat( vPlaneSize, 1, CV_8UC1, imageYV12.ptr() + yPlaneSize + vPlaneSize ).clone(),
            Mat( vPlaneSize, 1, CV_8UC1, imageYV12.ptr() + yPlaneSize ).clone()
        };
        Mat vuMat( vPlaneSize, 1, CV_8UC2, imageYV12.ptr() + yPlaneSize );
        merge(channels, 2, vuMat);

        writeBytes( imageYV12.ptr(), imageYV12.rows * imageYV12.cols );
        #endif

        drainEncoder(false);

        frameIndex++;
    }

    virtual bool open( const cv::String& filename, int fourcc, double fps, cv::Size frameSize, const VideoWriterParameters& params )
    {
        media_status_t status;

        close();

        const FourCCInfo* info = findInfo(fourcc);
        if (NULL == info) {
            LOGE("ERROR: findInfo");
            return false;
        }

        format = AMediaFormat_new();
        if (NULL == format) {
            LOGE("ERROR: AMediaFormat_new");
            goto error;
        }

        LOGV("mime: %s, width: %d, height: %d, fps: %f", info->mime, frameSize.width, frameSize.height, fps);

        AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, info->mime);
        AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, frameSize.width);
        AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, frameSize.height);
        AMediaFormat_setFloat(format, AMEDIAFORMAT_KEY_FRAME_RATE, (float)fps);
        AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, 5);
        AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, frameSize.width * frameSize.height * 5);
        AMediaFormat_setInt32(
            format, AMEDIAFORMAT_KEY_COLOR_FORMAT,
            #if __ANDROID_API__ >= 26
            COLOR_FormatSurface
            #else
            COLOR_FormatYUV420SemiPlanar
            #endif
        );

        encoder = AMediaCodec_createEncoderByType(info->mime);
        if (NULL == encoder) {
            LOGE("ERROR: AMediaCodec_createEncoderByType");
            goto error;
        }

        status = AMediaCodec_configure(encoder, format, NULL, NULL, AMEDIACODEC_CONFIGURE_FLAG_ENCODE);
        if (AMEDIA_OK != status) {
            LOGE("ERROR: AMediaCodec_configure (%d)", status);
            goto error;
        }

        #if __ANDROID_API__ >= 26
        status = AMediaCodec_createInputSurface(encoder, &surface);
        if (AMEDIA_OK != status || NULL == surface) {
            LOGE("ERROR: AMediaCodec_createInputSurface (%d)", status);
            goto error;
        }
        ANativeWindow_setBuffersGeometry(surface, width, height, AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM);
        #endif

        AMediaCodec_start(encoder);

        fd = ::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
        if (fd < 0) {
            LOGE("ERROR: open");
            goto error;
        }

        muxer = AMediaMuxer_new(fd, info->muxerFormat);
        if (NULL == muxer) {
            LOGE("ERROR: AMediaMuxer_new");
            goto error;
        }

        AMediaMuxer_setOrientationHint(muxer, params.get(CAP_PROP_ORIENTATION_META, 0));

        frameIndex = 0;
        width = frameSize.width;
        height = frameSize.height;
        frameRate = fps;
        videoTrackIndex = -1;

        return true;

    error:
        close();
        return false;
    }

    virtual void close()
    {
        if (videoTrackIndex >= 0 && NULL != muxer) {
            drainEncoder(true);
            AMediaMuxer_stop(muxer);
        }

        if (NULL != encoder) AMediaCodec_delete(encoder);
        if (NULL != muxer) AMediaMuxer_delete(muxer);

        #if __ANDROID_API__ >= 26
        if (NULL != surface) ANativeWindow_release(surface);
        #endif

        if (fd >= 0) ::close(fd);
        if (NULL != format) AMediaFormat_delete(format);

        format = NULL;
        encoder = NULL;
        muxer = NULL;
        #if __ANDROID_API__ >= 26
        surface = NULL;
        #endif
        frameIndex = 0;
        width = 0;
        height = 0;
        frameRate = 0.;
        videoTrackIndex = -1;
        fd = -1;
    }

    virtual double getProperty(int) const CV_OVERRIDE { return 0.; }
    virtual bool setProperty(int, double) CV_OVERRIDE { return false; }
    virtual bool isOpened() const CV_OVERRIDE { return NULL != encoder; }
};


const AndroidMediaNdkVideoWriter::FourCCInfo AndroidMediaNdkVideoWriter::FOURCC_INFO[] = {
    { CV_FOURCC('H', '2', '6', '4'), "video/avc", AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4 },
    { CV_FOURCC('H', '2', '6', '5'), "video/hevc", AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4 },
    { CV_FOURCC('H', '2', '6', '3'), "video/3gpp", AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4 },
    { CV_FOURCC('M', 'P', '4', 'V'), "video/mp4v-es", AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4 },
    { 0, NULL },
};



/****************** Implementation of interface functions ********************/

Ptr<IVideoCapture> cv::createAndroidCapture_file(const std::string &filename) {
    Ptr<AndroidMediaNdkCapture> res = makePtr<AndroidMediaNdkCapture>();
    if (res && res->initCapture(filename.c_str()))
        return res;
    return Ptr<IVideoCapture>();
}


Ptr<IVideoWriter> cv::createAndroidVideoWriter(
    const std::string& filename, int fourcc,
    double fps, const cv::Size& frameSize,
    const VideoWriterParameters& params) {
    Ptr<AndroidMediaNdkVideoWriter> writer = makePtr<AndroidMediaNdkVideoWriter>(filename, fourcc, fps, frameSize, params);
    if (writer && writer->isOpened())
        return writer;
    return Ptr<IVideoWriter>();
}