vl: implemented a few functions and made stubs to get mplayer running
[mesa.git] / src / gallium / state_trackers / vdpau / mpeg2_bitstream_parser.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "mpeg2_bitstream_parser.h"
29
30 int
31 vlVdpMPEG2NextStartCode(struct vdpMPEG2BitstreamParser *parser)
32 {
33 uint32_t integer = 0;
34 uint32_t bytes_to_end;
35
36 /* Move cursor to the start of a byte */
37 while(parser->cursor % 8)
38 parser->cursor++;
39
40 bytes_to_end = parser->cur_bitstream_length - parser->cursor/8 - 1;
41
42 /* Read byte after byte, until startcode is found */
43 while(integer != 0x00000100)
44 {
45 if (bytes_to_end < 0)
46 {
47 parser->state = MPEG2_HEADER_DONE;
48 return 1;
49 }
50
51 integer << 8;
52 integer = integer & (unsigned char)(parser->ptr_bitstream + parser->cursor/8)[0];
53
54 bytes_to_end--;
55 parser->cursor += 8;
56
57 }
58
59 /* start_code found. rewind cursor a byte */
60 parser->cursor -= 8;
61
62 return 0;
63 }
64
65 int
66 vlVdpMPEG2BitstreamToMacroblock (
67 struct pipe_screen *screen,
68 VdpBitstreamBuffer const *bitstream_buffers,
69 uint32_t bitstream_buffer_count,
70 unsigned int *num_macroblocks,
71 struct pipe_mpeg12_macroblock **pipe_macroblocks)
72 {
73 bool b_header_done = false;
74 struct vdpMPEG2BitstreamParser parser;
75
76 debug_printf("[VDPAU] Starting decoding MPEG2 stream");
77
78 num_macroblocks[0] = 0;
79
80 memset(&parser,0,sizeof(parser));
81 parser.state = MPEG2_HEADER_START_CODE;
82 parser.cur_bitstream_length = bitstream_buffers[0].bitstream_bytes;
83 parser.ptr_bitstream = (unsigned char *)bitstream_buffers[0].bitstream;
84
85 /* Main header parser loop */
86 while(!b_header_done)
87 {
88 switch (parser.state)
89 {
90 case MPEG2_HEADER_START_CODE:
91 if (vlVdpMPEG2NextStartCode(&parser))
92 continue;
93
94 /* Start_code found */
95 switch ((parser.ptr_bitstream + parser.cursor/8)[0])
96 {
97 /* sequence_header_code */
98 case 0xB3:
99 debug_printf("[VDPAU][Bitstream parser] Sequence header code found at cursor pos: %d\n", parser.cursor);
100 exit(1);
101 break;
102 }
103
104 break;
105 case MPEG2_HEADER_DONE:
106 debug_printf("[VDPAU][Bitstream parser] Done parsing current header\n");
107 break;
108
109 }
110
111
112 }
113
114
115 return 0;
116 }
117