[g3dvl] make ycbcr stream and block data a public interface
[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 #include <stdio.h>
28 #include <stdlib.h>
29 #include "mpeg2_bitstream_parser.h"
30
31 #if 0
32 int
33 vlVdpMPEG2NextStartCode(struct vdpMPEG2BitstreamParser *parser)
34 {
35 uint32_t integer = 0xffffff00;
36 uint8_t * ptr_read = parser->ptr_bitstream;
37 int8_t * bytes_to_end;
38
39 bytes_to_end = parser->ptr_bitstream_end - parser->ptr_bitstream;
40
41 /* Read byte after byte, until startcode is found */
42 while(integer != 0x00000100) {
43 if (bytes_to_end <= 0) {
44 parser->state = MPEG2_BITSTREAM_DONE;
45 parser->code = 0;
46 return 0;
47 }
48 integer = ( integer | *ptr_read++ ) << 8;
49 bytes_to_end--;
50 }
51 parser->ptr_bitstream = ptr_read;
52 parser->code = parser->ptr_bitstream;
53 /* start_code found. rewind cursor a byte */
54 //parser->cursor -= 8;
55
56 return 0;
57 }
58
59 int
60 vlVdpMPEG2BitstreamToMacroblock(struct pipe_screen *screen,
61 VdpBitstreamBuffer const *bitstream_buffers,
62 uint32_t bitstream_buffer_count,
63 unsigned int *num_macroblocks,
64 struct pipe_mpeg12_macroblock **pipe_macroblocks)
65 {
66 bool b_header_done = false;
67 struct vdpMPEG2BitstreamParser parser;
68
69 #if(1)
70 FILE *fp;
71
72 if ((fp = fopen("binout", "w"))==NULL) {
73 printf("Cannot open file.\n");
74 exit(1);
75 }
76 fwrite(bitstream_buffers[0].bitstream, 1, bitstream_buffers[0].bitstream_bytes, fp);
77 fclose(fp);
78
79 #endif
80
81 debug_printf("[VDPAU] Starting decoding MPEG2 stream\n");
82
83 num_macroblocks[0] = 0;
84
85 memset(&parser,0,sizeof(parser));
86 parser.state = MPEG2_HEADER_START_CODE;
87 parser.ptr_bitstream = (unsigned char *)bitstream_buffers[0].bitstream;
88 parser.ptr_bitstream_end = parser.ptr_bitstream + bitstream_buffers[0].bitstream_bytes;
89
90 /* Main header parser loop */
91 while(!b_header_done) {
92 switch (parser.state) {
93 case MPEG2_SEEK_HEADER:
94 if (vlVdpMPEG2NextStartCode(&parser))
95 exit(1);
96 break;
97 /* Start_code found */
98 switch (parser.code) {
99 /* sequence_header_code */
100 case 0xB3:
101 debug_printf("[VDPAU][Bitstream parser] Sequence header code found\n");
102 /* We dont need to read this, because we already have this information */
103 break;
104 case 0xB5:
105 debug_printf("[VDPAU][Bitstream parser] Extension start code found\n");
106 //exit(1);
107 break;
108 case 0xB8:
109 debug_printf("[VDPAU][Bitstream parser] Extension start code found\n");
110 //exit(1);
111 break;
112 }
113 break;
114 case MPEG2_BITSTREAM_DONE:
115 if (parser.cur_bitstream < bitstream_buffer_count - 1) {
116 debug_printf("[VDPAU][Bitstream parser] Done parsing current bitstream. Moving to the next\n");
117 parser.cur_bitstream++;
118 parser.ptr_bitstream = (unsigned char *)bitstream_buffers[parser.cur_bitstream].bitstream;
119 parser.ptr_bitstream_end = parser.ptr_bitstream + bitstream_buffers[parser.cur_bitstream].bitstream_bytes;
120 parser.state = MPEG2_HEADER_START_CODE;
121 }
122 else {
123 debug_printf("[VDPAU][Bitstream parser] Done with frame\n");
124 exit(0);
125 // return 0;
126 }
127 break;
128 }
129 }
130
131 return 0;
132 }
133
134 #endif