NDK硬解h264

有几点须注意,

  1. 不要让硬解码器自已绘制表面,也就是AMediaCodec_configure传入的surface应设为空。否则运行时间长了,会出现“weak global reference table overflow”的崩机错误。
  2. 我之前文章里也说过了,不要静态链编libmediandk.so,老的安卓机上没有这个库,要动态检测加载,详细过程见之后代码。
  3. AMediaCodec_dequeueInputBuffer不是每一次都返回成功,如果失败了而丢弃h264数据帧,则最终的播放效果就时不时出现花屏,正确处理见之后代码。
  4. 硬解码器解出的数据默认可能是NV12格式也可能是YV12格式,不同的手机不一样,预先配置解码器的"color-format"为19,也没用。为了与ffmpeg解出的数据一致为YU12格式,统一的我们自已转。

以下是源码,摘选自EuhatRtsp开源软件,经历过长时间拷机运行没问题。其网址是:http://euhat.com/rtsp.html


DecoderHard.h

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
#pragma once
 
#include "DecodeOp.h"
 
class AMediaCodec;
 
class EuhatDecoderHard : public EuhatDecoderBase
{
    int updateSpsAndPps();
 
    int spsChanged_;
    int ppsChanged_;
    char *sps_;
    char *pps_;
    int width_;
    int height_;
    int oriHeight_;
    int colorFormat_;
    int stride_;
    int sliceHeight_;
    int cropTop_;
    int cropBottom_;
    int cropLeft_;
    int cropRight_;
    int isCodecInited_;
    void *surface_;
    int surfaceChanged_;
 
public:
    EuhatDecoderHard();
    virtual ~EuhatDecoderHard();
 
    virtual int init(EuhatDecoderCallback callback, void *context);
    virtual int fini();
    virtual int updateSps(const char *sps);
    virtual int updatePps(const char *pps);
    virtual int updateWH(int width, int height);
    virtual int updateSurface(void *surface);
    virtual int decode(char *frame, int frameLen);
 
    static int canWork();
 
    AMediaCodec *mediaCodec_;
};


DecoderHard.cpp

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
#include "DecoderHard.h"
#include <media/NdkMediaCodec.h>
#include "CommonOp.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/system_properties.h>
#include <dlfcn.h>
#include "RtspOp.h"
#include "CommonOp.h"
 
#define MIN(_a,_b) ((_a) > (_b) ? (_b) : (_a))
 
typedef AMediaCodec *(*AMediaCodec_createCodecByName_t)(const char *name);
typedef AMediaCodec *(*AMediaCodec_createDecoderByType_t)(const char *mime_type);
typedef media_status_t (*AMediaCodec_stop_t)(AMediaCodec *);
typedef media_status_t (*AMediaCodec_delete_t)(AMediaCodec *);
typedef media_status_t (*AMediaCodec_configure_t)(
            AMediaCodec *,
            const AMediaFormat *format,
            ANativeWindow *surface,
            AMediaCrypto *crypto,
            uint32_t flags);
typedef media_status_t (*AMediaCodec_start_t)(AMediaCodec*);
typedef ssize_t (*AMediaCodec_dequeueInputBuffer_t)(AMediaCodec *, int64_t timeoutUs);
typedef uint8_t *(*AMediaCodec_getInputBuffer_t)(AMediaCodec *, size_t idx, size_t *out_size);
typedef media_status_t (*AMediaCodec_queueInputBuffer_t)(AMediaCodec *,
            size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags);
typedef ssize_t (*AMediaCodec_dequeueOutputBuffer_t)(AMediaCodec *, AMediaCodecBufferInfo *info, int64_t timeoutUs);
typedef uint8_t *(*AMediaCodec_getOutputBuffer_t)(AMediaCodec *, size_t idx, size_t *out_size);
typedef media_status_t (*AMediaCodec_releaseOutputBuffer_t)(AMediaCodec *, size_t idx, bool render);
typedef AMediaFormat *(*AMediaCodec_getOutputFormat_t)(AMediaCodec *);
 
typedef AMediaFormat *(*AMediaFormat_new_t)();
typedef media_status_t (*AMediaFormat_delete_t)(AMediaFormat *);
typedef bool (*AMediaFormat_getInt32_t)(AMediaFormat *, const char *name, int32_t *out);
typedef void (*AMediaFormat_setInt32_t)(AMediaFormat *, const char *name, int32_t value);
typedef void (*AMediaFormat_setString_t)(AMediaFormat *, const char *name, const char *value);
typedef void (*AMediaFormat_setBuffer_t)(AMediaFormat *, const char *name, void *data, size_t size);
 
#define API_LOOP(_m) \
    _m(AMediaCodec_createCodecByName) \
    _m(AMediaCodec_createDecoderByType) \
    _m(AMediaCodec_stop) \
    _m(AMediaCodec_delete) \
    _m(AMediaCodec_configure) \
    _m(AMediaCodec_start) \
    _m(AMediaCodec_dequeueInputBuffer) \
    _m(AMediaCodec_getInputBuffer) \
    _m(AMediaCodec_queueInputBuffer) \
    _m(AMediaCodec_dequeueOutputBuffer) \
    _m(AMediaCodec_getOutputBuffer) \
    _m(AMediaCodec_releaseOutputBuffer) \
    _m(AMediaCodec_getOutputFormat) \
    _m(AMediaFormat_new) \
    _m(AMediaFormat_delete) \
    _m(AMediaFormat_getInt32) \
    _m(AMediaFormat_setInt32) \
    _m(AMediaFormat_setString) \
    _m(AMediaFormat_setBuffer)
 
#define API_DEF(_name) _name##_t _name##_;
#define API_(_name) gApi._name##_
#define API_LOAD(_name) API_(_name) = (_name##_t)dlsym(handle, #_name);
 
struct CodecApi
{
    API_LOOP(API_DEF)
};
 
static const char *gAMEDIAFORMAT_KEY_WIDTH = "width";
static const char *gAMEDIAFORMAT_KEY_HEIGHT = "height";
static const char *gAMEDIAFORMAT_KEY_FRAME_RATE = "frame-rate";
 
static CodecApi gApi;
static int gIsApiLoaded = 0;
 
int EuhatDecoderHard::canWork()
{
    if (gIsApiLoaded)
        return 1;
 
    void *handle = dlopen("libmediandk.so", RTLD_LAZY);
    if (NULL == handle) {
        return 0;
    }
 
    API_LOOP(API_LOAD)
 
    gIsApiLoaded = 1;
/*  char* szError = dlerror();
    dlclose(handle);
*/
    return 1;
}
 
EuhatDecoderHard::EuhatDecoderHard()
{
    mediaCodec_ = NULL;
    spsChanged_ = 0;
    ppsChanged_ = 0;
    sps_ = pps_ = NULL;
    surfaceChanged_ = 0;
    isCodecInited_ = 0;
}
 
EuhatDecoderHard::~EuhatDecoderHard()
{
    if (isCodecInited_) {
        API_(AMediaCodec_stop)(mediaCodec_);
    }
    API_(AMediaCodec_delete)(mediaCodec_);
    mediaCodec_ = NULL;
}
 
int EuhatDecoderHard::init(EuhatDecoderCallback callback, void *context)
{
    callback_ = callback;
    context_ = context;
    surface_ = NULL;
 
    if (NULL == mediaCodec_) {
#if 1
        const char *mine = "video/avc";
        mediaCodec_ = API_(AMediaCodec_createDecoderByType)(mine);
        if (NULL == mediaCodec_) {
            DBG(("avc hard decoder is not supported.\n"));
            return 0;
        }
#else
        // const char *name = "OMX.qcom.video.decoder.avc";
        const char *name = "OMX.SEC.avc.sw.dec";
        // const char *name = "OMX.google.h264.decoder";
        mediaCodec_ = API_(AMediaCodec_createCodecByName)(name);
        if (NULL == mediaCodec_) {
            DBG(("hard decoder [%s] is not supported.\n", name));
            return 0;
        } else {
            DBG(("hard decoder [%s] is in work.\n", name));
        }
#endif
    }
 
    DBG(("codec is %p\n", mediaCodec_));
 
    return 1;
}
 
int EuhatDecoderHard::fini()
{
    spsChanged_ = 0;
    ppsChanged_ = 0;
    free(sps_); sps_ = NULL;
    free(pps_); pps_ = NULL;
 
    return 1;
}
 
int EuhatDecoderHard::updateSps(const char *sps)
{
    spsChanged_ = 1;
    free(sps_);
    sps_ = (char *)malloc((*(int *)sps) + 4);
    memcpy(sps_, sps, (*(int *)sps) + 4);
 
    updateSpsAndPps();
    return 1;
}
 
int EuhatDecoderHard::updatePps(const char *pps)
{
    ppsChanged_ = 1;
    free(pps_);
    pps_ = (char *)malloc((*(int *)pps) + 4);
    memcpy(pps_, pps, (*(int *)pps) + 4);
 
    updateSpsAndPps();
    return 1;
}
 
int EuhatDecoderHard::updateWH(int width, int height)
{
    width_ = width;
    height_ = height;
    return 1;
}
 
int EuhatDecoderHard::updateSurface(void *surface)
{
#if 1
    surface_ = NULL;
    return 1;
#endif
    if (surface_ != surface) {
        surfaceChanged_ = 1;
    }
    surface_ = surface;
    return 0;
}
 
int EuhatDecoderHard::updateSpsAndPps()
{
    if (spsChanged_ && !isCodecInited_) {
        media_status_t status;
        const char *mine = "video/avc";
        AMediaFormat *videoFormat = API_(AMediaFormat_new)();
        API_(AMediaFormat_setString)(videoFormat, "mime", mine);
        API_(AMediaFormat_setInt32)(videoFormat, gAMEDIAFORMAT_KEY_WIDTH, width_);
        API_(AMediaFormat_setInt32)(videoFormat, gAMEDIAFORMAT_KEY_HEIGHT, height_);
        API_(AMediaFormat_setInt32)(videoFormat, gAMEDIAFORMAT_KEY_FRAME_RATE, 25);
//      API_(AMediaFormat_setInt32)(videoFormat, "color-format", 19);
        API_(AMediaFormat_setBuffer)(videoFormat, "csd-0", sps_ + 4, *(int *)sps_);
//      API_(AMediaFormat_setBuffer)(videoFormat, "csd-1", pps_ + 4, *(int *)pps_);
        DBG(("codec %p, format %p, surface %p\n", mediaCodec_, videoFormat, surface_));
        status = API_(AMediaCodec_configure)(mediaCodec_, videoFormat, (ANativeWindow *)surface_, NULL, 0);
        API_(AMediaFormat_delete)(videoFormat);
        if (status != AMEDIA_OK) {
            DBG(("media codec configured failed, %d.\n", status));
            return 0;
        }
 
        colorFormat_ = 0;
        oriHeight_ = height_;
        stride_ = width_;
        sliceHeight_ = height_;
        cropTop_ = -1;
        cropBottom_ = -1;
        cropLeft_ = -1;
        cropRight_ = -1;
 
        status = API_(AMediaCodec_start)(mediaCodec_);
        if (status != AMEDIA_OK)
        {
            DBG(("media codec started failed, %d.\n", status));
            return 0;
        }
        DBG(("codec configured.\n"));
        isCodecInited_ = 1;
    }
    return 1;
}
 
static void nv212Yv12(char *nv21, char *yv12, int width, int height)
{
    int frameSize = width * height;
    memcpy(yv12, nv21, frameSize);
    nv21 += frameSize;
    yv12 += frameSize;
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    int quadFrame = halfWidth * halfHeight;
    for (int i = 0; i < halfHeight; i++) {
        for (int j = 0; j < halfWidth; j++) {
            *(yv12 + i * halfWidth + j) = *nv21++;
            *(yv12 + quadFrame + i * halfWidth + j) = *nv21++;
        }
    }
}
 
int EuhatDecoderHard::decode(char *frame, int frameLen)
{
    if (!isCodecInited_)
        return 0;
 
    ssize_t bufIdx;
    int times = 10;
    do {
        bufIdx = API_(AMediaCodec_dequeueInputBuffer)(mediaCodec_, 2000 * 100);
        if (bufIdx >= 0) {
            size_t outSize;
            uint8_t *inputBuf = API_(AMediaCodec_getInputBuffer)(mediaCodec_, bufIdx, &outSize);
            if (inputBuf != nullptr && frameLen <= outSize) {
                memcpy(inputBuf, frame, frameLen);
                media_status_t status = API_(AMediaCodec_queueInputBuffer)(mediaCodec_, bufIdx, 0, frameLen, 2000 /* pts */, 0);
            } else {
                DBG(("inputBuf %p, outSize %d, frameLen %d.\n", inputBuf, outSize, frameLen));
            }
            break;
        } else {
            whSleepMs(10);
            // DBG(("hard codec dequeue input buffer failed.\n"));
        }
    } while (--times > 0);
 
    if (times <= 0) {
        DBG(("hard codec dequeue input buffer failed still.\n"));
    }
 
    AMediaCodecBufferInfo info;
    ssize_t outBufIdx = API_(AMediaCodec_dequeueOutputBuffer)(mediaCodec_, &info, 2000);
    if (outBufIdx >= 0) {
        size_t outSize;
        uint8_t *outputBuf = API_(AMediaCodec_getOutputBuffer)(mediaCodec_, outBufIdx, &outSize);
        if (outputBuf != nullptr) {
 
            int sizeYUV = width_ * height_ * 3 / 2;
            char *yuv = (char *)outputMemPool_->alloc(sizeYUV + 4 * 3);
            *(int *)yuv = width_;
            *(int *)(yuv + 4) = height_;
            *(int *)(yuv + 8) = sizeYUV;
            char *dst = yuv + 4 * 3;
 
            /*
            DBG(("width %d, height %d, oriHeight %d, format %d, stride %d, sliceHeight %d, outSize %d, cropTop %d, cropBottom %d, cropLeft %d, cropRight %d.\n",
                width_, height_, oriHeight_, colorFormat_, stride_, sliceHeight_, outSize, cropTop_, cropBottom_, cropLeft_, cropRight_));
            */
 
            if (colorFormat_ != 21) {
                uint8_t *src = outputBuf + info.offset;
                memcpy(dst, src, width_ * height_);
                src += sliceHeight_ * stride_;
                dst += width_ * height_;
                memcpy(dst, src, width_ * height_ / 4);
                src += sliceHeight_ * stride_ / 4;
                dst += width_ * height_ / 4;
                memcpy(dst, src, width_ * height_ / 4);
            } else {
                uint8_t *src = outputBuf + info.offset;
                memcpy(dst, src, width_ * height_);
                src += sliceHeight_ * stride_;
                dst += width_ * height_;
                memcpy(dst, src, width_ * height_ / 2);
 
                char *yuvYv12 = (char *)outputMemPool_->alloc(sizeYUV + 4 * 3);
                *(int *)yuvYv12 = width_;
                *(int *)(yuvYv12 + 4) = height_;
                *(int *)(yuvYv12 + 8) = sizeYUV;
                nv212Yv12(yuv + 4 * 3, yuvYv12 + 4 * 3, width_, height_);
                outputMemPool_->dealloc(yuv);
                yuv = yuvYv12;
            }
 
            callback_(yuv, context_);
 
            API_(AMediaCodec_releaseOutputBuffer)(mediaCodec_, outBufIdx, info.size != 0);
            return 1;
        }
    } else {
        switch (outBufIdx) {
            case AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED:
            {
                AMediaFormat *format = API_(AMediaCodec_getOutputFormat)(mediaCodec_);
                // API_(AMediaFormat_getInt32)(format, "width", &width_);
                API_(AMediaFormat_getInt32)(format, "height", &oriHeight_);
                API_(AMediaFormat_getInt32)(format, "color-format", &colorFormat_); // 21 COLOR_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12
                API_(AMediaFormat_getInt32)(format, "stride", &stride_);
                API_(AMediaFormat_getInt32)(format, "slice-height", &sliceHeight_);
                API_(AMediaFormat_getInt32)(format, "crop-top", &cropTop_);
                API_(AMediaFormat_getInt32)(format, "crop-bottom", &cropBottom_);
                API_(AMediaFormat_getInt32)(format, "crop-left", &cropLeft_);
                API_(AMediaFormat_getInt32)(format, "crop-right", &cropRight_);
                API_(AMediaFormat_delete)(format);
 
                stride_ = stride_ > 0? stride_ : width_;
                sliceHeight_ = sliceHeight_ > 0? sliceHeight_ : height_;
 
                return 0;
            }
            case AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED:
                break;
            default:
                break;
        }
    }
    return 1;
}