#include "mpeg2_bitstream_parser.h"
-void
-vlVdpBitstreamToMacroblock (
+int
+vlVdpMPEG2NextStartCode(struct vdpMPEG2BitstreamParser *parser)
+{
+ uint32_t integer = 0;
+ uint32_t bytes_to_end;
+
+ /* Move cursor to the start of a byte */
+ while(parser->cursor % 8)
+ parser->cursor++;
+
+ bytes_to_end = parser->cur_bitstream_length - parser->cursor/8 - 1;
+
+ /* Read byte after byte, until startcode is found */
+ while(integer != 0x00000100)
+ {
+ if (bytes_to_end < 0)
+ {
+ parser->state = MPEG2_HEADER_DONE;
+ return 1;
+ }
+
+ integer << 8;
+ integer = integer & (unsigned char)(parser->ptr_bitstream + parser->cursor/8)[0];
+
+ bytes_to_end--;
+ parser->cursor += 8;
+
+ }
+
+ /* start_code found. rewind cursor a byte */
+ parser->cursor -= 8;
+
+ return 0;
+}
+
+int
+vlVdpMPEG2BitstreamToMacroblock (
struct pipe_screen *screen,
VdpBitstreamBuffer const *bitstream_buffers,
+ uint32_t bitstream_buffer_count,
unsigned int *num_macroblocks,
struct pipe_mpeg12_macroblock **pipe_macroblocks)
{
- debug_printf("[VDPAU] BitstreamToMacroblock not implemented yet");
- assert(0);
-
+ bool b_header_done = false;
+ struct vdpMPEG2BitstreamParser parser;
+
+ debug_printf("[VDPAU] Starting decoding MPEG2 stream");
+
+ num_macroblocks[0] = 0;
+
+ memset(&parser,0,sizeof(parser));
+ parser.state = MPEG2_HEADER_START_CODE;
+ parser.cur_bitstream_length = bitstream_buffers[0].bitstream_bytes;
+ parser.ptr_bitstream = (unsigned char *)bitstream_buffers[0].bitstream;
+
+ /* Main header parser loop */
+ while(!b_header_done)
+ {
+ switch (parser.state)
+ {
+ case MPEG2_HEADER_START_CODE:
+ if (vlVdpMPEG2NextStartCode(&parser))
+ continue;
+
+ /* Start_code found */
+ switch ((parser.ptr_bitstream + parser.cursor/8)[0])
+ {
+ /* sequence_header_code */
+ case 0xB3:
+ debug_printf("[VDPAU][Bitstream parser] Sequence header code found at cursor pos: %d\n", parser.cursor);
+ exit(1);
+ break;
+ }
+
+ break;
+ case MPEG2_HEADER_DONE:
+ debug_printf("[VDPAU][Bitstream parser] Done parsing current header\n");
+ break;
+
+ }
+
+
+ }
- return;
+ return 0;
}