1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 #include "mpeg2_bitstream_parser.h"
33 vlVdpMPEG2NextStartCode(struct vdpMPEG2BitstreamParser
*parser
)
35 uint32_t integer
= 0xffffff00;
36 uint8_t * ptr_read
= parser
->ptr_bitstream
;
37 int8_t * bytes_to_end
;
39 bytes_to_end
= parser
->ptr_bitstream_end
- parser
->ptr_bitstream
;
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
;
48 integer
= ( integer
| *ptr_read
++ ) << 8;
51 parser
->ptr_bitstream
= ptr_read
;
52 parser
->code
= parser
->ptr_bitstream
;
53 /* start_code found. rewind cursor a byte */
54 //parser->cursor -= 8;
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
)
66 bool b_header_done
= false;
67 struct vdpMPEG2BitstreamParser parser
;
72 if ((fp
= fopen("binout", "w"))==NULL
) {
73 printf("Cannot open file.\n");
76 fwrite(bitstream_buffers
[0].bitstream
, 1, bitstream_buffers
[0].bitstream_bytes
, fp
);
81 debug_printf("[VDPAU] Starting decoding MPEG2 stream\n");
83 num_macroblocks
[0] = 0;
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
;
90 /* Main header parser loop */
91 while(!b_header_done
) {
92 switch (parser
.state
) {
93 case MPEG2_SEEK_HEADER
:
94 if (vlVdpMPEG2NextStartCode(&parser
))
97 /* Start_code found */
98 switch (parser
.code
) {
99 /* sequence_header_code */
101 debug_printf("[VDPAU][Bitstream parser] Sequence header code found\n");
102 /* We dont need to read this, because we already have this information */
105 debug_printf("[VDPAU][Bitstream parser] Extension start code found\n");
109 debug_printf("[VDPAU][Bitstream parser] Extension start code found\n");
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
;
123 debug_printf("[VDPAU][Bitstream parser] Done with frame\n");