Merge pull request #218 from saper/build-fixes
[envytools.git] / vstream / test / test264.c
blobe4f4810b17c153a75867fd06e426fa1803c24ae5
1 #include "vstream.h"
2 #include "h264.h"
3 #include <stdio.h>
4 #include <stdlib.h>
6 int main(int argc, char **argv) {
7 FILE *out = 0;
8 if (argc >= 2) {
9 out = fopen(argv[1], "w");
10 if (!out) {
11 perror("fopen");
12 return 1;
15 struct bitstream *str = vs_new_encode(VS_H264);
16 uint32_t val;
17 val = 0xde;
18 if (vs_start(str, &val))
19 return 1;
21 struct h264_seqparm *seqparm = calloc(sizeof *seqparm, 1);
22 struct h264_picparm *picparm = calloc(sizeof *picparm, 1);
23 struct h264_slice *slice = calloc(sizeof *slice, 1);
24 seqparm->frame_mbs_only_flag = 0;
25 seqparm->direct_8x8_inference_flag = 0;
26 picparm->num_slice_groups_minus1 = 0;
27 picparm->constrained_intra_pred_flag = 0;
28 picparm->transform_8x8_mode_flag = 1;
29 picparm->entropy_coding_mode_flag = 1;
30 slice->seqparm = seqparm;
31 slice->picparm = picparm;
32 slice->sliceqpy = 26;
33 slice->pic_width_in_mbs = 8;
34 slice->pic_size_in_mbs = slice->pic_width_in_mbs * 24;
35 slice->first_mb_in_slice = 0;
36 slice->nal_unit_type = 1;
37 slice->slice_type = H264_SLICE_TYPE_P;
38 slice->mbaff_frame_flag = 0;
39 slice->field_pic_flag = 1;
40 slice->cabac_init_idc = 2;
41 slice->chroma_array_type = 1;
42 slice->num_ref_idx_l0_active_minus1 = 31;
43 slice->num_ref_idx_l1_active_minus1 = 31;
44 slice->bit_depth_luma_minus8 = 0;
45 slice->bit_depth_chroma_minus8 = 0;
46 slice->mbs = calloc(sizeof *slice->mbs, slice->pic_size_in_mbs);
47 int i, j, k;
48 for (i = 0; i < 96; i++) {
49 slice->mbs[i].mb_field_decoding_flag = i >> 2 & 1;
50 slice->mbs[i].coded_block_pattern = i * 3 % 48;
51 slice->mbs[i].transform_size_8x8_flag = i& 1;
52 slice->mbs[i].mb_qp_delta = (i%5) - 2;
53 if (i < 16) {
54 slice->mbs[i].mb_type = 0;
55 } else if (i < 40) {
56 slice->mbs[i].mb_type = 1 + (i- 16);
57 } else if (i >= 44 && slice->slice_type == H264_SLICE_TYPE_P) {
58 if (i < 48) {
59 slice->mbs[i].mb_type = H264_MB_TYPE_P_SKIP;
60 } else if (i < 72) {
61 int x = (i - 48)/8;
62 slice->mbs[i].mb_type = H264_MB_TYPE_P_L0_16X16 + x;
63 if (x == 0) {
64 for (j = 0; j < 4; j++)
65 slice->mbs[i].ref_idx[0][j] = 15 + (i & 7);
66 for (j = 0; j < 16; j++) {
67 slice->mbs[i].mvd[0][j][0] = -((i & 7) + 3);
68 slice->mbs[i].mvd[0][j][1] = (i&3);
70 } else if (x == 1) {
71 for (j = 0; j < 4; j++)
72 slice->mbs[i].ref_idx[0][j] = 4 + (j >> 1);
73 for (j = 0; j < 16; j++) {
74 slice->mbs[i].mvd[0][j][0] = 12 + (j >> 3);
75 slice->mbs[i].mvd[0][j][1] = -(j >> 3);
77 } else if (x == 2) {
78 for (j = 0; j < 4; j++)
79 slice->mbs[i].ref_idx[0][j] = 2 + (j & 1);
80 for (j = 0; j < 16; j++) {
81 slice->mbs[i].mvd[0][j][0] = 6 + (j>>2 & 1);
82 slice->mbs[i].mvd[0][j][1] = -(j>>2 & 1);
85 } else {
86 slice->mbs[i].mb_type = H264_MB_TYPE_P_8X8;
87 slice->mbs[i].transform_size_8x8_flag = 0;
88 for (j = 0; j < 4; j++) {
89 slice->mbs[i].sub_mb_type[j] = j;
90 slice->mbs[i].ref_idx[0][j] = j+1;
91 int tt[4] = { 0, 2, 1, 3};
92 for (k = 0; k < 4; k++) {
93 int kk = j * 4 + (k & j[tt]);
94 slice->mbs[i].mvd[0][j*4+k][0] = 16+kk;
95 slice->mbs[i].mvd[0][j*4+k][1] = kk - 32;;
99 } else if (i >= 44 && slice->slice_type == H264_SLICE_TYPE_B) {
100 if (i < 48) {
101 slice->mbs[i].mb_type = H264_MB_TYPE_B_SKIP;
102 } else if (i < 72) {
103 /* XXX */
104 slice->mbs[i].mb_type = H264_MB_TYPE_B_SKIP;
105 } else {
106 /* XXX */
107 slice->mbs[i].mb_type = H264_MB_TYPE_B_SKIP;
109 } else {
110 slice->mbs[i].mb_type = H264_MB_TYPE_I_PCM;
112 if (slice->mbs[i].mb_type == H264_MB_TYPE_I_NXN || slice->mbs[i].mb_type == H264_MB_TYPE_SI) {
113 for (j = 0; j < 16; j++) {
114 slice->mbs[i].prev_intra4x4_pred_mode_flag[j] = j & 1;
115 slice->mbs[i].rem_intra4x4_pred_mode[j] = j >> 1;
117 for (j = 0; j < 4; j++) {
118 slice->mbs[i].prev_intra8x8_pred_mode_flag[j] = j & 1;
119 slice->mbs[i].rem_intra8x8_pred_mode[j] = j >> 1;
122 if (slice->mbs[i].mb_type < H264_MB_TYPE_P_BASE) {
123 slice->mbs[i].intra_chroma_pred_mode = i >> 2 & 3;
125 if (slice->mbs[i].mb_type == H264_MB_TYPE_P_SKIP || slice->mbs[i].mb_type == H264_MB_TYPE_B_SKIP) {
126 slice->mbs[i].mb_field_decoding_flag = 0;
127 slice->mbs[i].coded_block_pattern = 0;
128 slice->mbs[i].transform_size_8x8_flag = 0;
129 slice->mbs[i].mb_qp_delta = 0;
130 slice->mbs[i].intra_chroma_pred_mode = 0;
131 } else if (slice->mbs[i].mb_type == H264_MB_TYPE_I_PCM) {
132 slice->mbs[i].coded_block_pattern = 0x2f;
133 slice->mbs[i].transform_size_8x8_flag = 0;
134 slice->mbs[i].mb_qp_delta = 0;
135 slice->mbs[i].intra_chroma_pred_mode = 0;
136 for (j = 0; j < 256; j++) {
137 if (i & 1)
138 slice->mbs[i].pcm_sample_chroma[j] = j;
139 if (i & 2)
140 slice->mbs[i].pcm_sample_luma[j] = j;
142 } else {
143 if (h264_is_intra_16x16_mb_type(slice->mbs[i].mb_type)) {
144 int mbt = slice->mbs[i].mb_type;
145 int infer_cbp = (((mbt - H264_MB_TYPE_I_16X16_0_0_0) >> 2) % 3) << 4;
146 if (mbt >= H264_MB_TYPE_I_16X16_0_0_1)
147 infer_cbp |= 0xf;
148 slice->mbs[i].coded_block_pattern = infer_cbp;
149 slice->mbs[i].transform_size_8x8_flag = 0;
150 for (j = 0; j < 16; j++) {
151 slice->mbs[i].block_luma_dc[0][j] = 0x100 + j;
152 if (slice->mbs[i].coded_block_pattern >> (j >> 2) & 1) {
153 for (k = 0; k < 15; k++) {
154 if (j) {
155 slice->mbs[i].block_luma_ac[0][j][k] = j * 16 + k + 1;
160 } else {
161 if (!slice->mbs[i].coded_block_pattern)
162 slice->mbs[i].mb_qp_delta = 0;
163 for (j = 0; j < 16; j++) {
164 if (slice->mbs[i].coded_block_pattern >> (j >> 2) & 1) {
165 for (k = 0; k < 16; k++) {
166 if (j) {
167 slice->mbs[i].block_luma_4x4[0][j][k] = j * 16 + k;
168 slice->mbs[i].block_luma_8x8[0][j>>2][(j&3)*16+k] = j*16 + k;
174 if (slice->mbs[i].coded_block_pattern & 0x30) {
175 for (k = 0; k < 4; k++) {
176 slice->mbs[i].block_chroma_dc[i&1][k] = -0x10 + k;
179 if (slice->mbs[i].coded_block_pattern & 0x20) {
180 for (j = 0; j < 4; j++) {
181 for (k = 0; k < 15; k++) {
182 if (j != 1)
183 slice->mbs[i].block_chroma_ac[i>>1&1][j][k] = k - 0x1000 + j * 0x100;
188 if (!slice->mbaff_frame_flag)
189 slice->mbs[i].mb_field_decoding_flag = slice->field_pic_flag;
191 slice->last_mb_in_slice = i - 1;
193 if (h264_slice_data(str, slice)) return 1;
195 if (out)
196 fwrite(str->bytes, str->bytesnum, 1, out);
198 struct bitstream *nstr = vs_new_decode(VS_H264, str->bytes, str->bytesnum);
199 if (vs_start(nstr, &val))
200 return 1;
201 if (val != 0xde) {
202 fprintf (stderr, "Fail 1\n");
203 return 1;
205 if (h264_slice_data(nstr, slice)) {
206 h264_print_slice_data(slice);
207 return 1;
209 h264_print_slice_data(slice);
211 fprintf (stderr, "All ok!\n");
213 return 0;