1 /*
2  * header.c
3  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7  * See http://libmpeg2.sourceforge.net/ for updates.
8  *
9  * mpeg2dec is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * mpeg2dec is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 /*
25 #define LOG_PAN_SCAN
26 */
27 
28 #include "config.h"
29 
30 #include <stdio.h>  /* For printf debugging */
31 #include <inttypes.h>
32 
33 #include "mpeg2_internal.h"
34 #include <xine/attributes.h>
35 
36 /* default intra quant matrix, in zig-zag order */
37 static const uint8_t default_intra_quantizer_matrix[64] ATTR_ALIGN(16) = {
38     8,
39     16, 16,
40     19, 16, 19,
41     22, 22, 22, 22,
42     22, 22, 26, 24, 26,
43     27, 27, 27, 26, 26, 26,
44     26, 27, 27, 27, 29, 29, 29,
45     34, 34, 34, 29, 29, 29, 27, 27,
46     29, 29, 32, 32, 34, 34, 37,
47     38, 37, 35, 35, 34, 35,
48     38, 38, 40, 40, 40,
49     48, 48, 46, 46,
50     56, 56, 58,
51     69, 69,
52     83
53 };
54 
55 uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) =
56 {
57     /* Zig-Zag scan pattern */
58      0, 1, 8,16, 9, 2, 3,10,
59     17,24,32,25,18,11, 4, 5,
60     12,19,26,33,40,48,41,34,
61     27,20,13, 6, 7,14,21,28,
62     35,42,49,56,57,50,43,36,
63     29,22,15,23,30,37,44,51,
64     58,59,52,45,38,31,39,46,
65     53,60,61,54,47,55,62,63
66 };
67 
68 uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) =
69 {
70     /* Alternate scan pattern */
71     0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
72     41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
73     51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
74     53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
75 };
76 
77 /* count must be between 1 and 32 */
get_bits(uint8_t * buffer,uint32_t count,uint32_t * bit_position)78 static uint32_t get_bits(uint8_t *buffer, uint32_t count, uint32_t *bit_position) {
79   uint32_t byte_offset;
80   uint32_t bit_offset;
81   uint32_t bit_mask;
82   uint32_t bit_bite;
83   uint32_t result=0;
84   if (count == 0) return 0;
85   do {
86     byte_offset = *bit_position >> 3;  /* Div 8 */
87     bit_offset = 8 - (*bit_position & 0x7); /* Bits got 87654321 */
88     bit_mask = ((1 << (bit_offset)) - 1);
89     bit_bite = bit_offset;
90     if (count < bit_offset) {
91       bit_mask ^=  ((1 << (bit_offset-count)) - 1);
92       bit_bite = count;
93     }
94     /*
95     printf("Byte=0x%02x Bitmask=0x%04x byte_offset=%u bit_offset=%u bit_byte=%u count=%u\n",buffer[byte_offset], bit_mask, byte_offset, bit_offset, bit_bite,count);
96     */
97     result = (result << bit_bite) | ((buffer[byte_offset] & bit_mask) >> (bit_offset-bit_bite));
98     *bit_position+=bit_bite;
99     count-=bit_bite;
100   } while ((count > 0) && (byte_offset<50) );
101   return result;
102 }
103 
get_bits_signed(uint8_t * buffer,uint32_t count,uint32_t * bit_position)104 static int32_t get_bits_signed(uint8_t *buffer, uint32_t count, uint32_t *bit_position) {
105   uint32_t value = get_bits(buffer, count, bit_position);
106   uint32_t sign_mask = (uint32_t)((uint32_t)-1 << (count - 1));
107   if (value & sign_mask)
108     value |= sign_mask; /* sign-extend value */
109   return (int32_t)value;
110 }
111 
mpeg2_header_state_init(picture_t * picture)112 void mpeg2_header_state_init (picture_t * picture)
113 {
114     picture->scan = mpeg2_scan_norm;
115     picture->load_intra_quantizer_matrix = 1;
116     picture->load_non_intra_quantizer_matrix = 1;
117 }
118 
mpeg2_header_sequence(picture_t * picture,uint8_t * buffer)119 int mpeg2_header_sequence (picture_t * picture, uint8_t * buffer)
120 {
121     int width, height;
122     int i;
123 
124     if ((buffer[6] & 0x20) != 0x20)
125 	return 1;	/* missing marker_bit */
126 
127     height = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
128 
129     picture->display_width = width = (height >> 12);
130     picture->display_height = height = (height & 0xfff);
131 
132     width = (width + 15) & ~15;
133     height = (height + 15) & ~15;
134 
135     if ((width > 1920) || (height > 1152))
136 	return 1;	/* size restrictions for MP@HL */
137 
138     picture->coded_picture_width = width;
139     picture->coded_picture_height = height;
140 
141     /* this is not used by the decoder */
142     picture->aspect_ratio_information = buffer[3] >> 4;
143     picture->frame_rate_code = buffer[3] & 15;
144     picture->bitrate = (buffer[4]<<10)|(buffer[5]<<2)|(buffer[6]>>6);
145 
146     if (buffer[7] & 2) {
147 	for (i = 0; i < 64; i++)
148 	    picture->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
149 		(buffer[i+7] << 7) | (buffer[i+8] >> 1);
150 	buffer += 64;
151     } else
152 	for (i = 0; i < 64; i++)
153 	    picture->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
154 		default_intra_quantizer_matrix [i];
155 
156     if (buffer[7] & 1)
157 	for (i = 0; i < 64; i++)
158 	    picture->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
159 		buffer[i+8];
160     else
161 	for (i = 0; i < 64; i++)
162 	    picture->non_intra_quantizer_matrix[i] = 16;
163     picture->load_intra_quantizer_matrix = 1;
164     picture->load_non_intra_quantizer_matrix = 1;
165     /* MPEG1 - for testing only */
166     picture->mpeg1 = 1;
167     picture->intra_dc_precision = 0;
168     picture->frame_pred_frame_dct = 1;
169     picture->q_scale_type = 0;
170     picture->concealment_motion_vectors = 0;
171     /* picture->alternate_scan = 0; */
172     picture->picture_structure = FRAME_PICTURE;
173     /* picture->second_field = 0; */
174 
175     return 0;
176 }
177 
sequence_extension(picture_t * picture,uint8_t * buffer)178 static int sequence_extension (picture_t * picture, uint8_t * buffer)
179 {
180     /* check chroma format, size extensions, marker bit */
181     if (((buffer[1] & 0x07) != 0x02) || (buffer[2] & 0xe0) ||
182 	((buffer[3] & 0x01) != 0x01))
183 	return 1;
184 
185     /* this is not used by the decoder */
186     picture->progressive_sequence = (buffer[1] >> 3) & 1;
187 
188     picture->low_delay = buffer[5] & 0x80;
189 
190     if (!picture->progressive_sequence)
191 	picture->coded_picture_height =
192 	    (picture->coded_picture_height + 31) & ~31;
193 
194 
195     /* printf ("libmpeg2: low_delay : %d\n", picture->low_delay); */
196 
197 /*
198     printf ("libmpeg2: sequence extension+5 : %08x (%d)\n",
199 	    buffer[5], buffer[5] % 0x80);
200  */
201 
202     picture->frame_rate_ext_n = buffer[5] & 0x31;
203     picture->frame_rate_ext_d = (buffer[5] >> 2) & 0x03;
204 
205     /* MPEG1 - for testing only */
206     picture->mpeg1 = 0;
207 
208     return 0;
209 }
210 
quant_matrix_extension(picture_t * picture,uint8_t * buffer)211 static int quant_matrix_extension (picture_t * picture, uint8_t * buffer)
212 {
213     int i;
214 
215     if (buffer[0] & 8) {
216 	for (i = 0; i < 64; i++)
217 	    picture->intra_quantizer_matrix[mpeg2_scan_norm[i]] =
218 		(buffer[i] << 5) | (buffer[i+1] >> 3);
219 	buffer += 64;
220     }
221 
222     if (buffer[0] & 4)
223 	for (i = 0; i < 64; i++)
224 	    picture->non_intra_quantizer_matrix[mpeg2_scan_norm[i]] =
225 		(buffer[i] << 6) | (buffer[i+1] >> 2);
226 
227     return 0;
228 }
229 
picture_coding_extension(picture_t * picture,uint8_t * buffer)230 static int picture_coding_extension (picture_t * picture, uint8_t * buffer)
231 {
232     /* pre subtract 1 for use later in compute_motion_vector */
233     picture->f_motion.f_code[0] = (buffer[0] & 15) - 1;
234     picture->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
235     picture->b_motion.f_code[0] = (buffer[1] & 15) - 1;
236     picture->b_motion.f_code[1] = (buffer[2] >> 4) - 1;
237 
238     picture->intra_dc_precision = (buffer[2] >> 2) & 3;
239     picture->picture_structure = buffer[2] & 3;
240     picture->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
241     picture->concealment_motion_vectors = (buffer[3] >> 5) & 1;
242     picture->q_scale_type = (buffer[3] >> 4) & 1;
243     picture->intra_vlc_format = (buffer[3] >> 3) & 1;
244 
245     if (buffer[3] & 4)	/* alternate_scan */
246 	picture->scan = mpeg2_scan_alt;
247     else
248 	picture->scan = mpeg2_scan_norm;
249 
250     /* these are not used by the decoder */
251     picture->top_field_first = buffer[3] >> 7;
252     picture->repeat_first_field = (buffer[3] >> 1) & 1;
253     picture->progressive_frame = buffer[4] >> 7;
254 
255     return 0;
256 }
257 
sequence_display_extension(picture_t * picture,uint8_t * buffer)258 static int sequence_display_extension (picture_t * picture, uint8_t * buffer) {
259   /* FIXME: implement. */
260   uint32_t bit_position;
261   /*uint32_t padding;*/
262 
263   bit_position = 0;
264   /*padding = get_bits(buffer, 4, &bit_position);*/ bit_position += 4;
265   picture->video_format = get_bits(buffer, 3, &bit_position);
266   picture->colour_description = get_bits(buffer, 1, &bit_position);
267   if(picture->colour_description) {
268   picture->colour_primatives = get_bits(buffer, 8, &bit_position);
269   picture->transfer_characteristics = get_bits(buffer, 8, &bit_position);
270   picture->matrix_coefficients = get_bits(buffer, 8, &bit_position);
271   }
272   picture->display_horizontal_size = get_bits(buffer, 14, &bit_position);
273   /*padding = get_bits(buffer, 1, &bit_position);*/ bit_position++;
274   picture->display_vertical_size = get_bits(buffer, 14, &bit_position);
275 
276 #ifdef LOG_PAN_SCAN
277   printf("Sequence_display_extension\n");
278   printf("     video_format: %u\n", picture->video_format);
279   printf("     colour_description: %u\n", picture->colour_description);
280   if(picture->colour_description) {
281   printf("     colour_primatives: %u\n", picture->colour_primatives);
282   printf("     transfer_characteristics %u\n", picture->transfer_characteristics);
283   printf("     matrix_coefficients %u\n", picture->matrix_coefficients);
284   }
285   printf("     display_horizontal_size %u\n", picture->display_horizontal_size);
286   printf("     display_vertical_size %u\n", picture->display_vertical_size);
287 #endif
288 
289   return 0;
290 }
291 
picture_display_extension(picture_t * picture,uint8_t * buffer)292 static int picture_display_extension (picture_t * picture, uint8_t * buffer) {
293   uint32_t bit_position;
294   /*uint32_t padding;*/
295 
296 #ifdef LOG_PAN_SCAN
297     printf ("libmpeg2: picture_display_extension\n");
298 #endif
299 
300   bit_position = 0;
301   /*padding = get_bits(buffer, 4, &bit_position);*/ bit_position += 4;
302   picture->frame_centre_horizontal_offset = get_bits_signed(buffer, 16, &bit_position);
303   /*padding = get_bits(buffer, 1, &bit_position);*/ bit_position++;
304   picture->frame_centre_vertical_offset = get_bits_signed(buffer, 16, &bit_position);
305   /*padding = get_bits(buffer, 1, &bit_position);*/ bit_position++;
306 
307 #ifdef LOG_PAN_SCAN
308   printf("Pan & Scan centre (x,y) = (%d, %d)\n",
309     picture->frame_centre_horizontal_offset,
310     picture->frame_centre_vertical_offset);
311 #endif
312 
313   return 0;
314 }
315 
mpeg2_header_extension(picture_t * picture,uint8_t * buffer)316 int mpeg2_header_extension (picture_t * picture, uint8_t * buffer)
317 {
318     switch (buffer[0] & 0xf0) {
319     case 0x00:	/* reserved */
320         return 0;
321 
322     case 0x10:	/* sequence extension */
323 	return sequence_extension (picture, buffer);
324 
325     case 0x20:	/* sequence display extension for Pan & Scan */
326 	return sequence_display_extension (picture, buffer);
327 
328     case 0x30:	/* quant matrix extension */
329 	return quant_matrix_extension (picture, buffer);
330 
331     case 0x40:	/* copyright extension */
332         return 0;
333 
334     case 0x50:	/* sequence scalable extension */
335         return 0;
336 
337     case 0x60:	/* reserved */
338         return 0;
339 
340     case 0x70:	/* picture display extension for Pan & Scan */
341 	return picture_display_extension (picture, buffer);
342 
343     case 0x80:	/* picture coding extension */
344 	return picture_coding_extension (picture, buffer);
345 
346     case 0x90:	/* picture spacial scalable extension */
347         return 0;
348 
349     case 0xA0:	/* picture temporal scalable extension */
350         return 0;
351 
352     case 0xB0:	/* camera parameters extension */
353         return 0;
354 
355     case 0xC0:	/* ITU-T extension */
356         return 0;
357 
358     case 0xD0:	/* reserved */
359         return 0;
360 
361     case 0xE0:	/* reserved */
362         return 0;
363 
364     case 0xF0:	/* reserved */
365         return 0;
366     }
367 
368     return 0;
369 }
370 
mpeg2_header_group_of_pictures(picture_t * picture,uint8_t * buffer)371 int mpeg2_header_group_of_pictures (picture_t * picture, uint8_t * buffer) {
372   uint32_t bit_position;
373   /*uint32_t padding;*/
374   bit_position = 0;
375 
376   picture->drop_frame_flag = get_bits(buffer, 1, &bit_position);
377   picture->time_code_hours = get_bits(buffer, 5, &bit_position);
378   picture->time_code_minutes = get_bits(buffer, 6, &bit_position);
379   /*padding = get_bits(buffer, 1, &bit_position);*/ bit_position++;
380   picture->time_code_seconds = get_bits(buffer, 6, &bit_position);
381   picture->time_code_pictures = get_bits(buffer, 6, &bit_position);
382   picture->closed_gop = get_bits(buffer, 1, &bit_position);
383   picture->broken_link = get_bits(buffer, 1, &bit_position);
384 
385 #ifdef LOG_PAN_SCAN
386   printf("Group of pictures\n");
387   printf("     drop_frame_flag: %u\n", picture->drop_frame_flag);
388   printf("     time_code: HH:MM:SS:Pictures %02u:%02u:%02u:%02u\n",
389          picture->time_code_hours,
390          picture->time_code_minutes,
391          picture->time_code_seconds,
392          picture->time_code_pictures);
393   printf("     closed_gop: %u\n", picture->closed_gop);
394   printf("     bloken_link: %u\n", picture->broken_link);
395 #endif
396 
397   return 0;
398 }
399 
mpeg2_header_picture(picture_t * picture,uint8_t * buffer)400 int mpeg2_header_picture (picture_t * picture, uint8_t * buffer)
401 {
402     picture->picture_coding_type = (buffer [1] >> 3) & 7;
403     picture->vbv_delay = ((buffer[1] << 13) | (buffer[2] << 5) |
404 			  (buffer[3] >> 3)) & 0xffff;
405 
406     /* forward_f_code and backward_f_code - used in mpeg1 only */
407     picture->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
408     picture->f_motion.f_code[0] =
409 	(((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
410     picture->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
411     picture->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
412 
413     /* move in header_process_picture_header */
414         picture->second_field =
415             (picture->picture_structure != FRAME_PICTURE) &&
416             !(picture->second_field);
417 
418     return 0;
419 }
420