forked from linux-sunxi/libvdpau-sunxi
-
Notifications
You must be signed in to change notification settings - Fork 8
/
mpeg12.c
208 lines (181 loc) · 7.05 KB
/
mpeg12.c
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
/*
* Copyright (c) 2013 Jens Kuske <jenskuske@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <string.h>
#include "vdpau_private.h"
#include "ve.h"
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#define TIMEMEAS 0
extern uint64_t get_time();
static const uint8_t zigzag_scan[64] =
{
0, 1, 5, 6, 14, 15, 27, 28,
2, 4, 7, 13, 16, 26, 29, 42,
3, 8, 12, 17, 25, 30, 41, 43,
9, 11, 18, 24, 31, 40, 44, 53,
10, 19, 23, 32, 39, 45, 52, 54,
20, 22, 33, 38, 46, 51, 55, 60,
21, 34, 37, 47, 50, 56, 59, 61,
35, 36, 48, 49, 57, 58, 62, 63
};
static int mpeg_find_startcode(CEDARV_MEMORY mem, int len)
{
int pos = 0;
uint8_t *data = cedarv_getPointer(mem);
while (pos < len)
{
int zeros = 0;
for ( ; pos < len; pos++)
{
if (data[pos] == 0x00)
zeros++;
else if (data[pos] == 0x01 && zeros >= 2)
{
pos++;
break;
}
else
zeros = 0;
}
uint8_t marker = data[pos++];
if (marker >= 0x01 && marker <= 0xaf)
return pos - 4;
}
return 0;
}
static unsigned long num_pics=0;
static unsigned long num_longs=0;
static VdpStatus mpeg12_decode(decoder_ctx_t *decoder, VdpPictureInfo const *_info, const int len, video_surface_ctx_t *output)
{
VdpPictureInfoMPEG1Or2 const *info = (VdpPictureInfoMPEG1Or2 const *)_info;
int start_offset = mpeg_find_startcode(decoder->data, len);
int i;
// activate MPEG engine
void *cedarv_regs = cedarv_get(CEDARV_ENGINE_MPEG, 0);
// set quantisation tables
for (i = 0; i < 64; i++)
writel((uint32_t)(64 + zigzag_scan[i]) << 8 | info->intra_quantizer_matrix[i], cedarv_regs + CEDARV_MPEG_IQ_MIN_INPUT);
for (i = 0; i < 64; i++)
writel((uint32_t)(zigzag_scan[i]) << 8 | info->non_intra_quantizer_matrix[i], cedarv_regs + CEDARV_MPEG_IQ_MIN_INPUT);
// set size
uint16_t width = (decoder->width + 15) / 16;
uint16_t height = (decoder->height + 15) / 16;
output->vidWidth = width * 16;
output->vidHeight = height * 16;
writel((width << 8) | height, cedarv_regs + CEDARV_MPEG_SIZE);
writel(((width * 16) << 16) | (height * 16), cedarv_regs + CEDARV_MPEG_FRAME_SIZE);
// set picture header
uint32_t pic_header = 0;
pic_header |= ((info->picture_coding_type & 0x7) << 28);
pic_header |= ((info->f_code[0][0] & 0xf) << 24);
pic_header |= ((info->f_code[0][1] & 0xf) << 20);
pic_header |= ((info->f_code[1][0] & 0xf) << 16);
pic_header |= ((info->f_code[1][1] & 0xf) << 12);
pic_header |= ((info->intra_dc_precision & 0x3) << 10);
pic_header |= ((info->picture_structure & 0x3) << 8);
pic_header |= ((info->top_field_first & 0x1) << 7);
pic_header |= ((info->frame_pred_frame_dct & 0x1) << 6);
pic_header |= ((info->concealment_motion_vectors & 0x1) << 5);
pic_header |= ((info->q_scale_type & 0x1) << 4);
pic_header |= ((info->intra_vlc_format & 0x1) << 3);
pic_header |= ((info->alternate_scan & 0x1) << 2);
pic_header |= ((info->full_pel_forward_vector & 0x1) << 1);
pic_header |= ((info->full_pel_backward_vector & 0x1) << 0);
if (decoder->profile == VDP_DECODER_PROFILE_MPEG1)
pic_header |= 0x000003c0;
writel(pic_header, cedarv_regs + CEDARV_MPEG_PIC_HDR);
// ??
writel(0x80000138 | ((cedarv_get_version() < 0x1680) << 7), cedarv_regs + CEDARV_MPEG_CTRL);
if (cedarv_get_version() >= 0x1680)
writel((0x1 << 30) | (0x1 << 28) , cedarv_regs + CEDARV_EXTRA_OUT_FMT_OFFSET);
// set forward/backward predicion buffers
if (info->forward_reference != VDP_INVALID_HANDLE)
{
video_surface_ctx_t *forward = handle_get(info->forward_reference);
if(forward)
{
writel(cedarv_virt2phys(forward->dataY), cedarv_regs + CEDARV_MPEG_FWD_LUMA);
writel(cedarv_virt2phys(forward->dataU)/* + forward->plane_size */, cedarv_regs + CEDARV_MPEG_FWD_CHROMA);
handle_release(info->forward_reference);
}
}
if (info->backward_reference != VDP_INVALID_HANDLE)
{
video_surface_ctx_t *backward = handle_get(info->backward_reference);
if(backward)
{
writel(cedarv_virt2phys(backward->dataY), cedarv_regs + CEDARV_MPEG_BACK_LUMA);
writel(cedarv_virt2phys(backward->dataU)/* + backward->plane_size*/, cedarv_regs + CEDARV_MPEG_BACK_CHROMA);
handle_release(info->backward_reference);
}
}
// set output buffers (Luma / Croma)
writel(cedarv_virt2phys(output->dataY), cedarv_regs + CEDARV_MPEG_REC_LUMA);
writel(cedarv_virt2phys(output->dataU)/* + output->plane_size*/, cedarv_regs + CEDARV_MPEG_REC_CHROMA);
writel(cedarv_virt2phys(output->dataY), cedarv_regs + CEDARV_MPEG_ROT_LUMA);
writel(cedarv_virt2phys(output->dataU)/* + output->plane_size*/, cedarv_regs + CEDARV_MPEG_ROT_CHROMA);
if(output->source_format == VDP_YCBCR_FORMAT_NV12)
{
int align = output->alignment[0];
writel(OUTPUT_FORMAT_NV12 | EXTRA_OUTPUT_FORMAT_NV12, cedarv_regs + CEDARV_OUTPUT_FORMAT);
writel((0x1 << 30) | (0x1 << 28) , cedarv_regs + CEDARV_EXTRA_OUT_FMT_OFFSET);
writel((ALIGN(output->width, align)/2 << 16) | ALIGN(output->width, align), cedarv_regs + CEDARV_OUTPUT_STRIDE);
writel((ALIGN(output->width, align)/2 << 16) | ALIGN(output->width, align), cedarv_regs + CEDARV_EXTRA_OUT_STRIDE);
}
// set input offset in bits
writel(start_offset * 8, cedarv_regs + CEDARV_MPEG_VLD_OFFSET);
// set input length in bits
writel((len - start_offset) * 8, cedarv_regs + CEDARV_MPEG_VLD_LEN);
// input end
uint32_t input_addr = cedarv_virt2phys(decoder->data);
writel(input_addr + VBV_SIZE - 1, cedarv_regs + CEDARV_MPEG_VLD_END);
// set input buffer
writel((input_addr & 0x0ffffff0) | (input_addr >> 28) | (0x7 << 28), cedarv_regs + CEDARV_MPEG_VLD_ADDR);
// trigger
writel((((decoder->profile == VDP_DECODER_PROFILE_MPEG1) ? 1 : 2) << 24) | 0x8000000f, cedarv_regs + CEDARV_MPEG_TRIGGER);
// wait for interrupt
++num_pics;
#if TIMEMEAS
uint64_t tv, tv2;
tv = get_time();
#endif
int status = cedarv_wait(1);
if (status <= 0) {
cedarv_VeReset();
}
#if TIMEMEAS
tv2 = get_time();
if (tv2-tv > 100000000) {
printf("cedarv_wait, longer than 10ms:%lld, pics=%ld, longs=%ld\n", tv2-tv, num_pics, ++num_longs);
}
#endif
// clean interrupt flag
writel(0x0000c00f, cedarv_regs + CEDARV_MPEG_STATUS);
// stop MPEG engine
cedarv_put();
output->frame_decoded = 1;
return VDP_STATUS_OK;
}
VdpStatus new_decoder_mpeg12(decoder_ctx_t *decoder)
{
decoder->decode = mpeg12_decode;
return VDP_STATUS_OK;
}