vaapi: add missing slice_horizontal_position for MPEG-2.
[hwdecode-demos:vj-hwdecode-demos.git] / src / vaapi_mpeg2.c
1 /*
2  *  vaapi_mpeg2.c - MPEG-2 decoding through VA API
3  *
4  *  hwdecode-demos (C) 2009-2010 Splitted-Desktop Systems
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "sysdeps.h"
22 #include "vaapi.h"
23 #include "vaapi_compat.h"
24 #include "mpeg2.h"
25
26 int decode(void)
27 {
28     VAAPIContext * const vaapi = vaapi_get_context();
29     VAPictureParameterBufferMPEG2 *pic_param;
30     VASliceParameterBufferMPEG2 *slice_param;
31     VAIQMatrixBufferMPEG2 *iq_matrix;
32     int i, slice_count;
33
34     MPEG2PictureInfo mpeg2_pic_info;
35     MPEG2SliceInfo mpeg2_slice_info;
36     MPEG2IQMatrix mpeg2_iq_matrix;
37     const uint8_t *mpeg2_slice_data;
38     unsigned int mpeg2_slice_data_size;
39
40     if (!vaapi)
41         return -1;
42
43     mpeg2_get_picture_info(&mpeg2_pic_info);
44
45     if (vaapi_init_decoder(VAProfileMPEG2Main, VAEntrypointVLD,
46                            mpeg2_pic_info.width, mpeg2_pic_info.height) < 0)
47         return -1;
48
49     if ((pic_param = vaapi_alloc_picture(sizeof(*pic_param))) == NULL)
50         return -1;
51
52 #define COPY(field) \
53     pic_param->field = mpeg2_pic_info.field
54 #define COPY_BFM(a,b,c) \
55     pic_param->BFM(a,b,c) = mpeg2_pic_info.a.b.c
56     pic_param->horizontal_size = mpeg2_pic_info.width;
57     pic_param->vertical_size = mpeg2_pic_info.height;
58     pic_param->forward_reference_picture = 0xffffffff;
59     pic_param->backward_reference_picture = 0xffffffff;
60     COPY(picture_coding_type);
61     COPY(f_code);
62     pic_param->BFV(picture_coding_extension, value) = 0; /* reset all bits */
63     COPY_BFM(picture_coding_extension, bits, intra_dc_precision);
64     COPY_BFM(picture_coding_extension, bits, picture_structure);
65     COPY_BFM(picture_coding_extension, bits, top_field_first);
66     COPY_BFM(picture_coding_extension, bits, frame_pred_frame_dct);
67     COPY_BFM(picture_coding_extension, bits, concealment_motion_vectors);
68     COPY_BFM(picture_coding_extension, bits, q_scale_type);
69     COPY_BFM(picture_coding_extension, bits, intra_vlc_format);
70     COPY_BFM(picture_coding_extension, bits, alternate_scan);
71     COPY_BFM(picture_coding_extension, bits, repeat_first_field);
72     COPY_BFM(picture_coding_extension, bits, progressive_frame);
73     COPY_BFM(picture_coding_extension, bits, is_first_field);
74 #undef COPY_BFM
75 #undef COPY
76
77     if ((iq_matrix = vaapi_alloc_iq_matrix(sizeof(*iq_matrix))) == NULL)
78         return -1;
79     mpeg2_get_iq_matrix(&mpeg2_iq_matrix);
80
81 #define COPY(field) iq_matrix->field = mpeg2_iq_matrix.field
82     COPY(load_intra_quantiser_matrix);
83     COPY(load_non_intra_quantiser_matrix);
84     COPY(load_chroma_intra_quantiser_matrix);
85     COPY(load_chroma_non_intra_quantiser_matrix);
86     for (i = 0; i < 64; i++) {
87         COPY(intra_quantiser_matrix[i]);
88         COPY(non_intra_quantiser_matrix[i]);
89         COPY(chroma_intra_quantiser_matrix[i]);
90         COPY(chroma_non_intra_quantiser_matrix[i]);
91     }
92 #undef COPY
93
94     slice_count = mpeg2_get_slice_count();
95     for (i = 0; i < slice_count; i++) {
96         if (mpeg2_get_slice_info(i, &mpeg2_slice_info) < 0)
97             return -1;
98         if (mpeg2_get_slice_data(i, &mpeg2_slice_data, &mpeg2_slice_data_size) < 0)
99             return -1;
100         if (mpeg2_slice_data_size != mpeg2_slice_info.slice_data_size)
101             return -1;
102         if ((slice_param = vaapi_alloc_slice(sizeof(*slice_param),
103                                              mpeg2_slice_data,
104                                              mpeg2_slice_data_size)) == NULL)
105             return -1;
106
107 #define COPY(field) slice_param->field = mpeg2_slice_info.field
108         COPY(macroblock_offset);
109 #if VA_CHECK_VERSION(0,32,0)
110         COPY(slice_horizontal_position);
111 #endif
112         COPY(slice_vertical_position);
113         COPY(quantiser_scale_code);
114         COPY(intra_slice_flag);
115 #undef COPY
116     }
117
118     return vaapi_decode();
119 }