hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
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
/*
*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* 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 MODULE_TAG "vp9d_api"
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include "mpp_mem.h"
#include "mpp_packet_impl.h"
 
#include "vp9d_codec.h"
#include "vp9d_parser.h"
#include "vp9d_api.h"
 
/*!
***********************************************************************
* \brief
*   alloc all buffer
***********************************************************************
*/
 
MPP_RET vp9d_init(void *ctx, ParserCfg *init)
{
    MPP_RET ret = MPP_OK;
    RK_U8 *buf = NULL;
    RK_S32 size = SZ_512K;
    Vp9CodecContext *vp9_ctx = (Vp9CodecContext *)ctx;
 
    if (!vp9_ctx || !init) {
        mpp_err("vp9d init fail");
        return MPP_ERR_NULL_PTR;
    }
 
    if ((ret = vp9d_parser_init(vp9_ctx, init)) != MPP_OK)
        goto _err_exit;
 
    if ((ret = vp9d_split_init(vp9_ctx)) != MPP_OK)
        goto _err_exit;
 
    buf = mpp_malloc(RK_U8, size);
    if (!buf) {
        mpp_err("vp9 init malloc stream buffer fail");
        ret = MPP_ERR_NOMEM;
        goto _err_exit;
    }
 
    if ((ret = mpp_packet_init(&vp9_ctx->pkt, (void *)buf, size)) != MPP_OK)
        goto _err_exit;
 
    return ret;
 
_err_exit:
    vp9d_deinit(vp9_ctx);
    return ret;
}
 
/*!
***********************************************************************
* \brief
*   free all buffer
***********************************************************************
*/
MPP_RET vp9d_deinit(void *ctx)
{
    RK_U8 *buf = NULL;
    Vp9CodecContext *vp9_ctx = (Vp9CodecContext *)ctx;
 
    if (vp9_ctx) {
        vp9d_parser_deinit(vp9_ctx);
        vp9d_split_deinit(vp9_ctx);
        if (vp9_ctx->pkt) {
            buf = mpp_packet_get_data(vp9_ctx->pkt);
            MPP_FREE(buf);
            mpp_packet_deinit(&vp9_ctx->pkt);
        }
    }
 
    return MPP_OK;
}
 
/*!
***********************************************************************
* \brief
*   reset
***********************************************************************
*/
MPP_RET  vp9d_reset(void *ctx)
{
    MPP_RET ret = MPP_ERR_UNKNOW;
 
    Vp9CodecContext *vp9_ctx = (Vp9CodecContext *)ctx;
    vp9d_paser_reset(vp9_ctx);
    return ret = MPP_OK;
}
 
/*!
***********************************************************************
* \brief
*   flush
***********************************************************************
*/
MPP_RET  vp9d_flush(void *ctx)
{
    MPP_RET ret = MPP_ERR_UNKNOW;
 
    (void)ctx;
    return ret = MPP_OK;
}
 
/*!
***********************************************************************
* \brief
*   prepare
***********************************************************************
*/
MPP_RET vp9d_prepare(void *ctx, MppPacket pkt, HalDecTask *task)
{
    MPP_RET ret = MPP_OK;
    Vp9CodecContext *vp9_ctx = (Vp9CodecContext *)ctx;
    SplitContext_t *ps = (SplitContext_t *)vp9_ctx->priv_data2;
    RK_S64 pts = -1;
    RK_S64 dts = -1;
    RK_U8 *buf = NULL;
    RK_S32 length = 0;
    RK_U8 *out_data = NULL;
    RK_S32 out_size = -1;
    RK_S32 consumed = 0;
    RK_U8 *pos = NULL;
    task->valid = -1;
 
    pts = mpp_packet_get_pts(pkt);
    dts = mpp_packet_get_dts(pkt);
    vp9_ctx->eos = mpp_packet_get_eos(pkt);
    buf = pos = mpp_packet_get_pos(pkt);
    length = (RK_S32)mpp_packet_get_length(pkt);
 
    consumed = vp9d_split_frame(ps, &out_data, &out_size, buf, length);
    pos += (consumed >= 0) ? consumed : length;
 
    mpp_packet_set_pos(pkt, pos);
    vp9d_dbg(VP9D_DBG_STRMIN, "pkt_len=%d, pts=%lld\n", length, pts);
    if (out_size > 0) {
        vp9d_get_frame_stream(vp9_ctx, out_data, out_size);
        task->input_packet = vp9_ctx->pkt;
        task->valid = 1;
        mpp_packet_set_pts(vp9_ctx->pkt, pts);
        mpp_packet_set_dts(vp9_ctx->pkt, dts);
        task->flags.eos = vp9_ctx->eos;
    } else {
        task->valid = 0;
        task->flags.eos = vp9_ctx->eos;
    }
 
    (void)pts;
    (void)dts;
    (void)task;
    return ret = MPP_OK;
}
 
 
/*!
***********************************************************************
* \brief
*   parser
***********************************************************************
*/
MPP_RET vp9d_parse(void *ctx, HalDecTask *in_task)
{
    Vp9CodecContext *vp9_ctx = (Vp9CodecContext *)ctx;
    MPP_RET ret = MPP_OK;
    vp9_parser_frame(vp9_ctx, in_task);
 
    return ret;
}
/*!
***********************************************************************
* \brief
*   callback
***********************************************************************
*/
MPP_RET vp9d_callback(void *decoder, void *info)
{
    MPP_RET ret = MPP_ERR_UNKNOW;
    Vp9CodecContext *vp9_ctx = (Vp9CodecContext *)decoder;
    vp9_parser_update(vp9_ctx, info);
 
    return ret = MPP_OK;
}
 
/*!
***********************************************************************
* \brief
*   api struct interface
***********************************************************************
*/
 
const ParserApi api_vp9d_parser = {
    .name = "vp9d_parse",
    .coding = MPP_VIDEO_CodingVP9,
    .ctx_size = sizeof(Vp9CodecContext),
    .flag = 0,
    .init = vp9d_init,
    .deinit = vp9d_deinit,
    .prepare = vp9d_prepare,
    .parse = vp9d_parse,
    .reset = vp9d_reset,
    .flush = vp9d_flush,
    .control = NULL,
    .callback = vp9d_callback,
};