/* * vidplayer * * Displays mpeg videos maybe */ #include #include #include #include #include #include #include "lib/window.h" #include "lib/graphics.h" static int buffer_size = 4096; static FILE * in_file = NULL;; static int total_offset = 0; static mpeg2dec_t * mpeg2dec; static gfx_context_t * ctx; static window_t * wina; static int _width; static int _height; static void draw_frame (uint8_t * const * buf, void * id) { for (int y = 0; y < (ctx->height > _height ? _height : ctx->height); ++y) { memcpy(&GFX(ctx, 0, y), &buf[0][y * _width * 4], 4 * (ctx->width > _width ? _width : ctx->width)); } for (int y = 0; y < ctx->height; ++y) { for (int x = 0; x < ctx->width; ++x) { GFX(ctx, x, y) = GFX(ctx, x, y) | 0xFF000000; } } flip(ctx); } static void decode_mpeg2(uint8_t * current, uint8_t * end) { const mpeg2_info_t * info; mpeg2_state_t state; mpeg2_buffer(mpeg2dec, current, end); total_offset += end - current; info = mpeg2_info(mpeg2dec); while (1) { state = mpeg2_parse(mpeg2dec); switch (state) { case STATE_BUFFER: return; case STATE_SEQUENCE: _width = info->sequence->width; _height = info->sequence->height; wina = window_create(100, 100, _width, _height); ctx = init_graphics_window_double_buffer(wina); draw_fill(ctx, rgb(127,127,127)); if (mpeg2_convert(mpeg2dec, mpeg2convert_rgb32, NULL)) { fprintf(stderr, "Color conversion failed?\n"); exit(1); } mpeg2_skip(mpeg2dec, 0); break; case STATE_PICTURE: break; case STATE_SLICE: case STATE_END: case STATE_INVALID_END: if (info->display_fbuf) { draw_frame(info->display_fbuf->buf, info->display_fbuf->id); } break; default: break; } } } static void * malloc_hook(unsigned size, mpeg2_alloc_t reason) { void * buf; if ((int)reason < 0) { return NULL; } buf = mpeg2_malloc(size, (mpeg2_alloc_t) - 1); if (buf && (reason == MPEG2_ALLOC_YUV || reason == MPEG2_ALLOC_CONVERTED)) { memset(buf, 0, size); } return buf; } int main (int argc, char ** argv) { if (argc < 2) { printf("usage: %s file\n", argv[0]); return -1; } in_file = fopen(argv[1], "rb"); //mpeg2_accel (0); mpeg2dec = mpeg2_init(); mpeg2_malloc_hooks(malloc_hook, NULL); setup_windowing(); /* Do something with a window */ uint8_t * buffer = (uint8_t *)malloc(buffer_size); uint8_t * end; do { w_keyboard_t * kbd = poll_keyboard_async(); if (kbd != NULL) { if (kbd->key == 'q') { break; } free(kbd); } /* MPEG2 decoding here */ end = buffer + fread(buffer, 1, buffer_size, in_file); decode_mpeg2(buffer, end); } while (end == buffer + buffer_size); free(buffer); fclose(in_file); teardown_windowing(); return 0; }