2007-01-01 12:59:32 +00:00
|
|
|
/*
|
|
|
|
* mpegplayer.c - based on :
|
|
|
|
* - mpeg2dec.c
|
|
|
|
* - m2psd.c (http://www.brouhaha.com/~eric/software/m2psd/)
|
2006-08-07 22:11:07 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
|
|
|
|
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
2007-01-01 12:59:32 +00:00
|
|
|
*
|
|
|
|
* m2psd: MPEG 2 Program Stream Demultiplexer
|
|
|
|
* Copyright (C) 2003 Eric Smith <eric@brouhaha.com>
|
2006-08-07 22:11:07 +00:00
|
|
|
*
|
|
|
|
* This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
|
|
|
|
* See http://libmpeg2.sourceforge.net/ for updates.
|
|
|
|
*
|
|
|
|
* mpeg2dec is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpeg2dec is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
NOTES:
|
|
|
|
|
|
|
|
mpegplayer is structured as follows:
|
|
|
|
|
|
|
|
1) Video thread (running on the COP for PortalPlayer targets).
|
|
|
|
2) Audio thread (running on the main CPU to maintain consistency with
|
|
|
|
the audio FIQ hander on PP).
|
|
|
|
3) The main thread which takes care of buffering.
|
|
|
|
|
|
|
|
Using the main thread for buffering wastes the 8KB main stack which is
|
|
|
|
in IRAM. However, 8KB is not enough for the audio thread to run (it
|
|
|
|
needs somewhere between 8KB and 9KB), so we create a new thread in
|
|
|
|
order to`give it a larger stack.
|
|
|
|
|
|
|
|
We use 4.5KB of the main stack for a libmad buffer (making use of
|
|
|
|
otherwise unused IRAM). There is also the possiblity of stealing the
|
|
|
|
main Rockbox codec thread's 9KB of IRAM stack and using that for
|
|
|
|
mpegplayer's audio thread - but we should only implement that if we
|
|
|
|
can put the IRAM to good use.
|
|
|
|
|
|
|
|
The button loop (and hence pause/resume, main menu and, in the future,
|
|
|
|
seeking) is placed in the audio thread. This keeps it on the main CPU
|
|
|
|
in PP targets and also allows buffering to continue in the background
|
|
|
|
whilst the main thread is filling the buffer.
|
|
|
|
|
|
|
|
A/V sync is not yet implemented but is planned to be achieved by
|
|
|
|
syncing the master clock with the audio, and then (as is currently
|
|
|
|
implemented), syncing video with the master clock. This can happen in
|
|
|
|
the audio thread, along with resyncing after pause.
|
|
|
|
|
|
|
|
Seeking should probably happen in the main thread, as that's where the
|
|
|
|
buffering happens.
|
|
|
|
|
|
|
|
On PortalPlayer targets, the main CPU is not being fully utilised -
|
|
|
|
the bottleneck is the video decoding on the COP. One way to improve
|
|
|
|
that might be to move the rendering of the frames (i.e. the
|
|
|
|
lcd_yuv_blit() call) from the COP back to the main CPU. Ideas and
|
|
|
|
patches for that are welcome!
|
|
|
|
|
|
|
|
Notes about MPEG files:
|
|
|
|
|
|
|
|
MPEG System Clock is 27MHz - i.e. 27000000 ticks/second.
|
|
|
|
|
|
|
|
FPS is represented in terms of a frame period - this is always an
|
|
|
|
integer number of 27MHz ticks.
|
|
|
|
|
|
|
|
e.g. 29.97fps (30000/1001) NTSC video has an exact frame period of
|
|
|
|
900900 27MHz ticks.
|
|
|
|
|
|
|
|
In libmpeg2, info->sequence->frame_period contains the frame_period.
|
|
|
|
|
|
|
|
Working with Rockbox's 100Hz tick, the common frame rates would need
|
|
|
|
to be as follows:
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
FPS | 27Mhz | 100Hz | 44.1KHz | 48KHz
|
|
|
|
--------|-----------------------------------------------------------
|
|
|
|
10* | 2700000 | 10 | 4410 | 4800
|
|
|
|
12* | 2250000 | 8.3333 | 3675 | 4000
|
|
|
|
15* | 1800000 | 6.6667 | 2940 | 3200
|
|
|
|
23.9760 | 1126125 | 4.170833333 | 1839.3375 | 2002
|
|
|
|
24 | 1125000 | 4.166667 | 1837.5 | 2000
|
|
|
|
25 | 1080000 | 4 | 1764 | 1920
|
|
|
|
29.9700 | 900900 | 3.336667 | 1471,47 | 1601.6
|
|
|
|
30 | 900000 | 3.333333 | 1470 | 1600
|
2007-01-01 12:59:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
*Unofficial framerates
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
#include "mpeg2dec_config.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#include "mpeg2.h"
|
2006-08-20 23:12:56 +00:00
|
|
|
#include "mpeg_settings.h"
|
2006-08-07 22:11:07 +00:00
|
|
|
#include "video_out.h"
|
2007-01-01 12:59:32 +00:00
|
|
|
#include "../../codecs/libmad/mad.h"
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
PLUGIN_HEADER
|
2006-11-18 02:18:29 +00:00
|
|
|
PLUGIN_IRAM_DECLARE
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
/* button definitions */
|
|
|
|
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
|
2006-08-20 23:12:56 +00:00
|
|
|
#define MPEG_MENU BUTTON_MODE
|
2006-08-09 10:47:22 +00:00
|
|
|
#define MPEG_STOP BUTTON_OFF
|
|
|
|
#define MPEG_PAUSE BUTTON_ON
|
2007-03-18 17:04:44 +00:00
|
|
|
#define MPEG_VOLDOWN BUTTON_DOWN
|
|
|
|
#define MPEG_VOLUP BUTTON_UP
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IPOD_3G_PAD) || (CONFIG_KEYPAD == IPOD_4G_PAD)
|
2006-08-20 23:12:56 +00:00
|
|
|
#define MPEG_MENU BUTTON_MENU
|
|
|
|
#define MPEG_PAUSE (BUTTON_PLAY | BUTTON_REL)
|
|
|
|
#define MPEG_STOP (BUTTON_PLAY | BUTTON_REPEAT)
|
2007-03-18 17:04:44 +00:00
|
|
|
#define MPEG_VOLDOWN BUTTON_SCROLL_BACK
|
|
|
|
#define MPEG_VOLUP BUTTON_SCROLL_FWD
|
2006-08-09 10:47:22 +00:00
|
|
|
|
2007-03-16 23:02:39 +00:00
|
|
|
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
|
2006-08-20 23:12:56 +00:00
|
|
|
#define MPEG_MENU (BUTTON_REC | BUTTON_REL)
|
2006-08-09 10:47:22 +00:00
|
|
|
#define MPEG_STOP BUTTON_POWER
|
|
|
|
#define MPEG_PAUSE BUTTON_PLAY
|
2007-03-18 17:04:44 +00:00
|
|
|
#define MPEG_VOLDOWN BUTTON_DOWN
|
|
|
|
#define MPEG_VOLUP BUTTON_UP
|
2006-08-09 10:47:22 +00:00
|
|
|
|
2006-08-09 13:25:43 +00:00
|
|
|
#elif CONFIG_KEYPAD == GIGABEAT_PAD
|
2006-08-20 23:12:56 +00:00
|
|
|
#define MPEG_MENU BUTTON_MENU
|
2006-08-09 13:25:43 +00:00
|
|
|
#define MPEG_STOP BUTTON_A
|
|
|
|
#define MPEG_PAUSE BUTTON_SELECT
|
2007-03-18 17:04:44 +00:00
|
|
|
#define MPEG_VOLDOWN BUTTON_DOWN
|
|
|
|
#define MPEG_VOLUP BUTTON_UP
|
|
|
|
#define MPEG_VOLDOWN2 BUTTON_VOL_DOWN
|
|
|
|
#define MPEG_VOLUP2 BUTTON_VOL_UP
|
2006-08-09 13:25:43 +00:00
|
|
|
|
2006-08-11 08:35:27 +00:00
|
|
|
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
|
2006-08-20 23:12:56 +00:00
|
|
|
#define MPEG_MENU (BUTTON_REW | BUTTON_REL)
|
2006-08-11 08:35:27 +00:00
|
|
|
#define MPEG_STOP BUTTON_POWER
|
|
|
|
#define MPEG_PAUSE BUTTON_PLAY
|
2007-03-18 17:04:44 +00:00
|
|
|
#define MPEG_VOLDOWN BUTTON_SCROLL_DOWN
|
|
|
|
#define MPEG_VOLUP BUTTON_SCROLL_UP
|
2006-08-11 08:35:27 +00:00
|
|
|
|
2006-10-26 13:38:09 +00:00
|
|
|
#elif CONFIG_KEYPAD == SANSA_E200_PAD
|
|
|
|
#define MPEG_MENU BUTTON_SELECT
|
|
|
|
#define MPEG_STOP BUTTON_POWER
|
|
|
|
#define MPEG_PAUSE BUTTON_UP
|
2007-03-18 17:04:44 +00:00
|
|
|
#define MPEG_VOLDOWN BUTTON_SCROLL_UP
|
|
|
|
#define MPEG_VOLUP BUTTON_SCROLL_DOWN
|
2006-10-26 13:38:09 +00:00
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
#else
|
|
|
|
#error MPEGPLAYER: Unsupported keypad
|
|
|
|
#endif
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
struct plugin_api* rb;
|
|
|
|
|
|
|
|
static mpeg2dec_t * mpeg2dec;
|
|
|
|
static int total_offset = 0;
|
|
|
|
|
|
|
|
/* Streams */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t* curr_packet; /* Current stream packet beginning */
|
|
|
|
uint8_t* curr_packet_end; /* Current stream packet end */
|
|
|
|
|
|
|
|
uint8_t* next_packet; /* Next stream packet beginning */
|
|
|
|
|
|
|
|
int id;
|
|
|
|
} Stream;
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
static Stream audio_str IBSS_ATTR;
|
|
|
|
static Stream video_str IBSS_ATTR;
|
2007-01-01 12:59:32 +00:00
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
/* NOTE: Putting the following variables in IRAM cause audio corruption
|
|
|
|
on the ipod (reason unknown)
|
|
|
|
*/
|
2007-01-01 12:59:32 +00:00
|
|
|
static uint8_t *disk_buf, *disk_buf_end;
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
static struct event_queue msg_queue IBSS_ATTR;
|
|
|
|
|
|
|
|
#define MSG_BUFFER_NEARLY_EMPTY 1
|
|
|
|
#define MSG_EXIT_REQUESTED 2
|
|
|
|
|
|
|
|
/* Threads */
|
|
|
|
static struct thread_entry* audiothread_id;
|
|
|
|
static struct thread_entry* videothread_id;
|
|
|
|
|
|
|
|
/* Status */
|
|
|
|
#define STREAM_PLAYING 0
|
|
|
|
#define STREAM_DONE 1
|
|
|
|
#define STREAM_PAUSING 2
|
2007-03-22 09:18:46 +00:00
|
|
|
#define STREAM_BUFFERING 3
|
|
|
|
#define STREAM_ERROR 4
|
|
|
|
#define PLEASE_STOP 5
|
|
|
|
#define PLEASE_PAUSE 6
|
2007-01-01 12:59:32 +00:00
|
|
|
|
|
|
|
int audiostatus IBSS_ATTR;
|
|
|
|
int videostatus IBSS_ATTR;
|
|
|
|
|
|
|
|
/* Various buffers */
|
|
|
|
/* TODO: Can we reduce the PCM buffer size? */
|
|
|
|
#define PCMBUFFER_SIZE (512*1024)
|
|
|
|
#define AUDIOBUFFER_SIZE (32*1024)
|
|
|
|
#define LIBMPEG2BUFFER_SIZE (2*1024*1024)
|
|
|
|
|
|
|
|
static void button_loop(void)
|
2006-08-09 10:47:22 +00:00
|
|
|
{
|
2006-08-20 23:12:56 +00:00
|
|
|
bool result;
|
2007-03-18 17:04:44 +00:00
|
|
|
int vol, minvol, maxvol;
|
2006-08-09 10:47:22 +00:00
|
|
|
int button = rb->button_get(false);
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
switch (button)
|
|
|
|
{
|
2007-03-18 17:04:44 +00:00
|
|
|
case MPEG_VOLUP:
|
|
|
|
case MPEG_VOLUP|BUTTON_REPEAT:
|
|
|
|
#ifdef MPEG_VOLUP2
|
|
|
|
case MPEG_VOLUP2:
|
|
|
|
case MPEG_VOLUP2|BUTTON_REPEAT:
|
|
|
|
#endif
|
|
|
|
vol = rb->global_settings->volume;
|
|
|
|
maxvol = rb->sound_max(SOUND_VOLUME);
|
|
|
|
|
|
|
|
if (vol < maxvol) {
|
|
|
|
vol++;
|
|
|
|
rb->sound_set(SOUND_VOLUME, vol);
|
|
|
|
rb->global_settings->volume = vol;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MPEG_VOLDOWN:
|
|
|
|
case MPEG_VOLDOWN|BUTTON_REPEAT:
|
|
|
|
#ifdef MPEG_VOLDOWN2
|
|
|
|
case MPEG_VOLDOWN2:
|
|
|
|
case MPEG_VOLDOWN2|BUTTON_REPEAT:
|
|
|
|
#endif
|
|
|
|
vol = rb->global_settings->volume;
|
|
|
|
minvol = rb->sound_min(SOUND_VOLUME);
|
|
|
|
|
|
|
|
if (vol > minvol) {
|
|
|
|
vol--;
|
|
|
|
rb->sound_set(SOUND_VOLUME, vol);
|
|
|
|
rb->global_settings->volume = vol;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-08-20 23:12:56 +00:00
|
|
|
case MPEG_MENU:
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->pcm_play_pause(false);
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) {
|
|
|
|
videostatus=PLEASE_PAUSE;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
/* Wait for video thread to stop */
|
|
|
|
while (videostatus == PLEASE_PAUSE) { rb->sleep(HZ/25); }
|
|
|
|
}
|
2006-08-20 23:12:56 +00:00
|
|
|
result = mpeg_menu();
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* The menu can change the font, so restore */
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
audiostatus = PLEASE_STOP;
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) videostatus = PLEASE_STOP;
|
2007-01-01 12:59:32 +00:00
|
|
|
} else {
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) videostatus = STREAM_PLAYING;
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->pcm_play_pause(true);
|
|
|
|
}
|
|
|
|
break;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
case MPEG_STOP:
|
2007-01-01 12:59:32 +00:00
|
|
|
audiostatus = PLEASE_STOP;
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) videostatus = PLEASE_STOP;
|
2007-01-01 12:59:32 +00:00
|
|
|
break;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
case MPEG_PAUSE:
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) videostatus=PLEASE_PAUSE;
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->pcm_play_pause(false);
|
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
button = BUTTON_NONE;
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(false);
|
|
|
|
#endif
|
|
|
|
do {
|
|
|
|
button = rb->button_get(true);
|
2007-01-01 12:59:32 +00:00
|
|
|
if (button == MPEG_STOP) {
|
|
|
|
audiostatus = PLEASE_STOP;
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) videostatus = PLEASE_STOP;
|
2007-01-01 12:59:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-08-09 10:47:22 +00:00
|
|
|
} while (button != MPEG_PAUSE);
|
2007-01-01 12:59:32 +00:00
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) videostatus = STREAM_PLAYING;
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->pcm_play_pause(true);
|
2006-08-09 10:47:22 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(true);
|
|
|
|
#endif
|
|
|
|
break;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2006-08-09 10:47:22 +00:00
|
|
|
default:
|
2007-01-01 12:59:32 +00:00
|
|
|
if(rb->default_event_handler(button) == SYS_USB_CONNECTED) {
|
|
|
|
audiostatus = PLEASE_STOP;
|
2007-03-22 09:18:46 +00:00
|
|
|
if (videostatus != STREAM_DONE) videostatus = PLEASE_STOP;
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|
2006-08-09 10:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* libmad related functions/definitions */
|
|
|
|
#define INPUT_CHUNK_SIZE 8192
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
struct mad_stream stream IBSS_ATTR;
|
|
|
|
struct mad_frame frame IBSS_ATTR;
|
|
|
|
struct mad_synth synth IBSS_ATTR;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
unsigned char mad_main_data[MAD_BUFFER_MDLEN]; /* 2567 bytes */
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
static void init_mad(void* mad_frame_overlap)
|
|
|
|
{
|
|
|
|
rb->memset(&stream, 0, sizeof(struct mad_stream));
|
|
|
|
rb->memset(&frame, 0, sizeof(struct mad_frame));
|
|
|
|
rb->memset(&synth, 0, sizeof(struct mad_synth));
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
mad_stream_init(&stream);
|
|
|
|
mad_frame_init(&frame);
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* We do this so libmad doesn't try to call codec_calloc() */
|
|
|
|
frame.overlap = mad_frame_overlap;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->memset(mad_main_data, 0, sizeof(mad_main_data));
|
|
|
|
stream.main_data = &mad_main_data;
|
|
|
|
}
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* MPEG related headers */
|
|
|
|
uint8_t packet_start_code_prefix [3] = { 0x00, 0x00, 0x01 };
|
|
|
|
uint8_t end_code [4] = { 0x00, 0x00, 0x01, 0xb9 };
|
|
|
|
uint8_t pack_start_code [4] = { 0x00, 0x00, 0x01, 0xba };
|
|
|
|
uint8_t system_header_start_code [4] = { 0x00, 0x00, 0x01, 0xbb };
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* This function demux the streams and give the next stream data pointer */
|
|
|
|
static void get_next_data( Stream* str )
|
|
|
|
{
|
|
|
|
uint8_t *p;
|
|
|
|
uint8_t *header;
|
|
|
|
int stream;
|
|
|
|
|
|
|
|
static int mpeg1_skip_table[16] = {
|
|
|
|
0, 0, 4, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
if (str->curr_packet_end == NULL) {
|
|
|
|
/* What does this do? */
|
2007-01-01 12:59:32 +00:00
|
|
|
while( (p = disk_buf) == NULL )
|
|
|
|
{
|
2007-03-22 09:18:46 +00:00
|
|
|
rb->lcd_putsxy(0,LCD_HEIGHT-10,"FREEZE!");
|
|
|
|
rb->lcd_update();
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->sleep(100);
|
|
|
|
}
|
2007-03-22 09:18:46 +00:00
|
|
|
} else {
|
2007-01-01 12:59:32 +00:00
|
|
|
p = str->curr_packet_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
int length, bytes;
|
|
|
|
|
|
|
|
/* Pack header, skip it */
|
|
|
|
if( rb->memcmp (p, pack_start_code, sizeof (pack_start_code)) == 0 )
|
|
|
|
{
|
|
|
|
if ((p[4] & 0xc0) == 0x40) { /* mpeg-2 */
|
|
|
|
p += 14 + (p[13] & 7);
|
|
|
|
} else if ((p[4] & 0xf0) == 0x20) { /* mpeg-1 */
|
|
|
|
p += 12;
|
|
|
|
} else {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash( 30, "Weird Pack header!" );
|
2007-01-01 12:59:32 +00:00
|
|
|
p += 5;
|
|
|
|
}
|
2007-03-16 21:56:08 +00:00
|
|
|
/*rb->splash( 30, "Pack header" );*/
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* System header, parse and skip it */
|
|
|
|
if( rb->memcmp (p, system_header_start_code, sizeof (system_header_start_code)) == 0 )
|
|
|
|
{
|
|
|
|
int header_length;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
p += 4; /*skip start code*/
|
|
|
|
header_length = (*(p++)) << 8;
|
|
|
|
header_length += *(p++);
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
p += header_length;
|
2007-03-16 21:56:08 +00:00
|
|
|
/*rb->splash( 30, "System header" );*/
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Packet header, parse it */
|
|
|
|
if( rb->memcmp (p, packet_start_code_prefix, sizeof (packet_start_code_prefix)) != 0 )
|
|
|
|
{
|
|
|
|
/* Problem */
|
2007-03-16 21:56:08 +00:00
|
|
|
//rb->splash( HZ*3, "missing packet start code prefix : %X%X at %X", *p, *(p+2), p-disk_buf );
|
2007-01-01 12:59:32 +00:00
|
|
|
str->curr_packet_end = str->curr_packet = NULL;
|
|
|
|
return;
|
|
|
|
//++p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We retrieve basic infos */
|
|
|
|
stream = *(p+3);
|
|
|
|
length = (*(p+4)) << 8;
|
|
|
|
length += *(p+5);
|
|
|
|
|
2007-03-16 21:56:08 +00:00
|
|
|
/*rb->splash( 100, "Stream : %X", stream );*/
|
2007-01-01 12:59:32 +00:00
|
|
|
if (stream != str->id)
|
|
|
|
{
|
|
|
|
/* End of stream ? */
|
|
|
|
if( stream == 0xB9 )
|
|
|
|
{
|
|
|
|
str->curr_packet_end = str->curr_packet = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's not the packet we're looking for, skip it */
|
|
|
|
p += length+6;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ok, it's our packet */
|
|
|
|
str->curr_packet_end = p + length+6;
|
|
|
|
header = p;
|
|
|
|
if ((header[6] & 0xc0) == 0x80) { /* mpeg2 */
|
|
|
|
|
|
|
|
length = 9 + header[8];
|
|
|
|
/* header points to the mpeg2 pes header */
|
|
|
|
if (header[7] & 0x80) {
|
|
|
|
uint32_t pts, dts;
|
|
|
|
|
|
|
|
pts = (((header[9] >> 1) << 30) |
|
|
|
|
(header[10] << 22) | ((header[11] >> 1) << 15) |
|
|
|
|
(header[12] << 7) | (header[13] >> 1));
|
|
|
|
dts = (!(header[7] & 0x40) ? pts :
|
|
|
|
((uint32_t)(((header[14] >> 1) << 30) |
|
|
|
|
(header[15] << 22) |
|
|
|
|
((header[16] >> 1) << 15) |
|
|
|
|
(header[17] << 7) | (header[18] >> 1))));
|
2007-03-22 09:18:46 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
if( stream >= 0xe0 )
|
|
|
|
mpeg2_tag_picture (mpeg2dec, pts, dts);
|
|
|
|
}
|
|
|
|
} else { /* mpeg1 */
|
|
|
|
int len_skip;
|
|
|
|
uint8_t * ptsbuf;
|
|
|
|
|
|
|
|
length = 7;
|
|
|
|
while (header[length - 1] == 0xff)
|
|
|
|
{
|
|
|
|
length++;
|
|
|
|
if (length > 23)
|
|
|
|
{
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash( 30, "Too much stuffing" );
|
2007-01-01 12:59:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((header[length - 1] & 0xc0) == 0x40)
|
|
|
|
{
|
|
|
|
length += 2;
|
|
|
|
}
|
|
|
|
len_skip = length;
|
|
|
|
length += mpeg1_skip_table[header[length - 1] >> 4];
|
|
|
|
|
|
|
|
/* header points to the mpeg1 pes header */
|
|
|
|
ptsbuf = header + len_skip;
|
|
|
|
if ((ptsbuf[-1] & 0xe0) == 0x20)
|
|
|
|
{
|
|
|
|
uint32_t pts, dts;
|
|
|
|
|
|
|
|
pts = (((ptsbuf[-1] >> 1) << 30) |
|
|
|
|
(ptsbuf[0] << 22) | ((ptsbuf[1] >> 1) << 15) |
|
|
|
|
(ptsbuf[2] << 7) | (ptsbuf[3] >> 1));
|
|
|
|
dts = (((ptsbuf[-1] & 0xf0) != 0x30) ? pts :
|
|
|
|
((uint32_t)(((ptsbuf[4] >> 1) << 30) |
|
|
|
|
(ptsbuf[5] << 22) | ((ptsbuf[6] >> 1) << 15) |
|
|
|
|
(ptsbuf[7] << 7) | (ptsbuf[18] >> 1))));
|
|
|
|
if( stream >= 0xe0 )
|
|
|
|
mpeg2_tag_picture (mpeg2dec, pts, dts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += length;
|
|
|
|
bytes = 6 + (header[4] << 8) + header[5] - length;
|
|
|
|
if (bytes > 0) {
|
|
|
|
/*str->curr_packet_end = p+bytes;*/
|
|
|
|
str->curr_packet = p;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( str->curr_packet_end > disk_buf_end )
|
|
|
|
{
|
|
|
|
/* We should ask for buffering here */
|
|
|
|
str->curr_packet_end = str->curr_packet = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* mpa_buffer;
|
|
|
|
size_t mpa_buffer_size;
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
static volatile int madpcm_playing IBSS_ATTR;
|
|
|
|
static volatile int16_t* pcm_buffer IBSS_ATTR;
|
|
|
|
static volatile size_t pcm_buffer_size IBSS_ATTR;
|
|
|
|
|
|
|
|
static volatile size_t pcmbuf_len IBSS_ATTR;
|
|
|
|
static volatile int16_t* pcmbuf_end IBSS_ATTR;
|
|
|
|
static volatile int16_t* pcmbuf_head IBSS_ATTR;
|
|
|
|
static volatile int16_t* pcmbuf_tail IBSS_ATTR;
|
2007-01-01 12:59:32 +00:00
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
static volatile uint32_t samplesplayed IBSS_ATTR;
|
2007-01-01 12:59:32 +00:00
|
|
|
|
|
|
|
static void init_pcmbuf(void)
|
|
|
|
{
|
|
|
|
pcmbuf_head = pcm_buffer;
|
|
|
|
pcmbuf_len = 0;
|
|
|
|
pcmbuf_tail = pcm_buffer;
|
|
|
|
pcmbuf_end = pcm_buffer + pcm_buffer_size / sizeof(int16_t);
|
|
|
|
madpcm_playing = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_more(unsigned char** start, size_t* size)
|
|
|
|
{
|
|
|
|
if (pcmbuf_len < 32*1024) {
|
|
|
|
*start = NULL;
|
|
|
|
*size = 0;
|
|
|
|
madpcm_playing = 0;
|
2007-03-22 09:18:46 +00:00
|
|
|
pcmbuf_len = 0;
|
2007-01-01 12:59:32 +00:00
|
|
|
} else {
|
|
|
|
*start = (unsigned char*)(pcmbuf_tail);
|
|
|
|
*size = 32*1024;
|
|
|
|
pcmbuf_tail += (32*1024)/sizeof(int16_t);
|
|
|
|
pcmbuf_len -= 32*1024;
|
|
|
|
if (pcmbuf_tail >= pcmbuf_end) { pcmbuf_tail = pcm_buffer; }
|
2007-03-22 09:18:46 +00:00
|
|
|
|
|
|
|
/* Update master clock */
|
|
|
|
samplesplayed += (32*1024)/4;
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int line;
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
static void audio_thread(void)
|
2007-01-01 12:59:32 +00:00
|
|
|
{
|
|
|
|
int32_t* left;
|
|
|
|
int32_t* right;
|
2007-03-22 22:47:04 +00:00
|
|
|
int32_t sample;
|
2007-01-01 12:59:32 +00:00
|
|
|
int i;
|
|
|
|
size_t n = 0;
|
|
|
|
size_t len;
|
|
|
|
int file_end = 0; /* A count of the errors in each frame */
|
|
|
|
int framelength;
|
|
|
|
|
2007-01-02 13:36:17 +00:00
|
|
|
/* We need this here to init the EMAC for Coldfire targets */
|
|
|
|
mad_synth_init(&synth);
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
init_pcmbuf();
|
|
|
|
|
|
|
|
/* This is the decoding loop. */
|
|
|
|
for (;;) {
|
|
|
|
button_loop();
|
|
|
|
|
|
|
|
if (audiostatus == PLEASE_STOP) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < 1000) { /* TODO: What is the maximum size of an MPEG audio frame? */
|
|
|
|
get_next_data( &audio_str );
|
|
|
|
if (audio_str.curr_packet == NULL) {
|
2007-03-22 09:18:46 +00:00
|
|
|
/* Wait for audio to finish */
|
|
|
|
while (pcmbuf_len > 0) { rb->sleep(HZ/10); }
|
2007-01-01 12:59:32 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
len = audio_str.curr_packet_end - audio_str.curr_packet;
|
|
|
|
if (n + len > mpa_buffer_size) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash( 30, "Audio buffer overflow" );
|
2007-01-01 12:59:32 +00:00
|
|
|
audiostatus=STREAM_DONE;
|
|
|
|
/* Wait to be killed */
|
|
|
|
for (;;) { rb->sleep(HZ); }
|
|
|
|
}
|
|
|
|
rb->memcpy(mpa_buffer+n,audio_str.curr_packet,len);
|
|
|
|
n += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock buffers */
|
|
|
|
if (stream.error == 0) {
|
|
|
|
mad_stream_buffer(&stream, mpa_buffer, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mad_frame_decode(&frame, &stream)) {
|
|
|
|
if (stream.error == MAD_FLAG_INCOMPLETE
|
|
|
|
|| stream.error == MAD_ERROR_BUFLEN) {
|
|
|
|
/* This makes the codec support partially corrupted files */
|
|
|
|
if (file_end == 30)
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* The mpa.c version: */
|
|
|
|
if (stream.next_frame)
|
|
|
|
inputbuffer = stream.next_frame;
|
|
|
|
else
|
|
|
|
inputbuffer++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
stream.error = 0;
|
|
|
|
file_end++;
|
|
|
|
continue;
|
|
|
|
} else if (MAD_RECOVERABLE(stream.error)) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* Some other unrecoverable error */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_end = 0;
|
|
|
|
|
|
|
|
mad_synth_frame(&synth, &frame);
|
|
|
|
|
|
|
|
/* TODO: Don't memmove so much... */
|
|
|
|
if (stream.next_frame) {
|
|
|
|
len = stream.next_frame - mpa_buffer;
|
|
|
|
rb->memmove(mpa_buffer,stream.next_frame,n-len);
|
|
|
|
n -= len;
|
|
|
|
} else {
|
|
|
|
/* What to do here? */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
/* The mpa.c version: */
|
|
|
|
if (stream.next_frame)
|
|
|
|
inputbuffer = stream.next_frame;
|
|
|
|
else
|
|
|
|
inputbuffer = inputbuffer_end;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
framelength = synth.pcm.length;
|
|
|
|
|
|
|
|
if (framelength > 0) {
|
|
|
|
/* Leave at least 32KB free (this will be the currently playing chunk) */
|
|
|
|
while (pcmbuf_len + framelength*4 + 32*1024 > pcm_buffer_size) { rb->yield(); }
|
|
|
|
|
|
|
|
if (MAD_NCHANNELS(&frame.header) == 2) {
|
|
|
|
left = &synth.pcm.samples[0][0];
|
|
|
|
right = &synth.pcm.samples[1][0];
|
|
|
|
for (i = 0 ; i < framelength; i++) {
|
2007-03-22 22:47:04 +00:00
|
|
|
/* libmad outputs s3.28 */
|
|
|
|
sample = *(left++) >> 13;
|
|
|
|
if (sample > 32767)
|
|
|
|
sample = 32767;
|
|
|
|
else if (sample < -32768)
|
|
|
|
sample = -32768;
|
|
|
|
*(pcmbuf_head++) = sample;
|
|
|
|
|
|
|
|
sample = *(right++) >> 13;
|
|
|
|
if (sample > 32767)
|
|
|
|
sample = 32767;
|
|
|
|
else if (sample < -32768)
|
|
|
|
sample = -32768;
|
|
|
|
*(pcmbuf_head++) = sample;
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
if (pcmbuf_head >= pcmbuf_end) { pcmbuf_head = pcm_buffer; }
|
|
|
|
}
|
|
|
|
} else { /* mono */
|
|
|
|
left = &synth.pcm.samples[0][0];
|
|
|
|
for (i = 0 ; i < framelength; i++) {
|
|
|
|
sample = *(left++) >> 13;
|
2007-03-22 22:47:04 +00:00
|
|
|
|
|
|
|
if (sample > 32767)
|
|
|
|
sample = 32767;
|
|
|
|
else if (sample < -32768)
|
|
|
|
sample = -32768;
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
*(pcmbuf_head++) = sample;
|
|
|
|
*(pcmbuf_head++) = sample;
|
|
|
|
if (pcmbuf_head >= pcmbuf_end) { pcmbuf_head = pcm_buffer; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Disable interrupts for Coldfire? */
|
|
|
|
|
|
|
|
/* pcmbuf_len is also modified by the FIQ handler (in
|
|
|
|
get_more), so we disable the FIQ TODO: Add sempahore so we
|
|
|
|
don't change whilst get_more is running. */
|
|
|
|
|
|
|
|
#ifdef CPU_ARM
|
|
|
|
disable_fiq();
|
|
|
|
#endif
|
|
|
|
pcmbuf_len += framelength*4;
|
|
|
|
#ifdef CPU_ARM
|
|
|
|
enable_fiq();
|
|
|
|
#endif
|
|
|
|
if ((!madpcm_playing) && (pcmbuf_len > 64*1024)) {
|
|
|
|
madpcm_playing = 1;
|
|
|
|
rb->pcm_play_data(get_more,NULL,0);
|
2007-03-22 09:18:46 +00:00
|
|
|
audiostatus = STREAM_PLAYING;
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rb->yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
rb->pcm_play_stop();
|
|
|
|
audiostatus=STREAM_DONE;
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
for (;;) {
|
|
|
|
button_loop();
|
|
|
|
rb->sleep(HZ/4);
|
|
|
|
}
|
2007-01-01 12:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of libmad stuff */
|
|
|
|
|
|
|
|
static uint64_t eta IBSS_ATTR;
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* TODO: Running in the main thread, libmad needs 8.25KB of stack.
|
|
|
|
The codec thread uses a 9KB stack. So we can probable reduce this a
|
|
|
|
little, but leave at 9KB for now to be safe. */
|
|
|
|
#define AUDIO_STACKSIZE (9*1024)
|
|
|
|
uint32_t audio_stack[AUDIO_STACKSIZE / sizeof(uint32_t)] IBSS_ATTR;
|
|
|
|
|
|
|
|
/* TODO: Check if 4KB is appropriate - it works for my test streams,
|
|
|
|
so maybe we can reduce it. */
|
|
|
|
#define VIDEO_STACKSIZE (4*1024)
|
|
|
|
static uint32_t video_stack[VIDEO_STACKSIZE / sizeof(uint32_t)] IBSS_ATTR;
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
static void video_thread(void)
|
2006-08-07 22:11:07 +00:00
|
|
|
{
|
|
|
|
const mpeg2_info_t * info;
|
|
|
|
mpeg2_state_t state;
|
2006-08-20 23:12:56 +00:00
|
|
|
char str[80];
|
2007-03-22 09:18:46 +00:00
|
|
|
int skipped = 0;
|
2007-01-01 12:59:32 +00:00
|
|
|
int skipcount = 0;
|
2007-03-22 09:18:46 +00:00
|
|
|
int frame = 0;
|
|
|
|
int lasttick;
|
2006-08-20 23:12:56 +00:00
|
|
|
unsigned int eta2;
|
2007-03-22 09:18:46 +00:00
|
|
|
unsigned int s;
|
2006-08-20 23:12:56 +00:00
|
|
|
int fps;
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
rb->sleep(HZ/5);
|
|
|
|
mpeg2dec = mpeg2_init ();
|
|
|
|
|
|
|
|
if (mpeg2dec == NULL) {
|
|
|
|
videostatus = STREAM_ERROR;
|
|
|
|
rb->splash(0, "mpeg2_init failed");
|
|
|
|
/* Commit suicide */
|
|
|
|
rb->remove_thread(NULL);
|
2006-08-20 23:12:56 +00:00
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
/* Clear the display - this is mainly just to indicate that the
|
|
|
|
video thread has started successfully. */
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
rb->lcd_update();
|
|
|
|
|
|
|
|
/* Used to decide when to display FPS */
|
|
|
|
lasttick = *rb->current_tick - HZ;
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* Request the first packet data */
|
|
|
|
get_next_data( &video_str );
|
|
|
|
mpeg2_buffer (mpeg2dec, video_str.curr_packet, video_str.curr_packet_end);
|
|
|
|
total_offset += video_str.curr_packet_end - video_str.curr_packet;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
info = mpeg2_info (mpeg2dec);
|
|
|
|
while (1) {
|
2007-01-01 12:59:32 +00:00
|
|
|
if (videostatus == PLEASE_STOP) {
|
|
|
|
goto done;
|
|
|
|
} else if (videostatus == PLEASE_PAUSE) {
|
|
|
|
videostatus = STREAM_PAUSING;
|
|
|
|
while (videostatus == STREAM_PAUSING) { rb->sleep(HZ/10); }
|
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
state = mpeg2_parse (mpeg2dec);
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->yield();
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case STATE_BUFFER:
|
2007-01-01 12:59:32 +00:00
|
|
|
/* Request next packet data */
|
|
|
|
get_next_data( &video_str );
|
|
|
|
mpeg2_buffer (mpeg2dec, video_str.curr_packet, video_str.curr_packet_end);
|
|
|
|
total_offset += video_str.curr_packet_end - video_str.curr_packet;
|
|
|
|
info = mpeg2_info (mpeg2dec);
|
|
|
|
if (video_str.curr_packet == NULL) {
|
|
|
|
/* No more data. */
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
case STATE_SEQUENCE:
|
2006-08-17 18:35:11 +00:00
|
|
|
vo_setup(info->sequence->width,
|
|
|
|
info->sequence->height,
|
|
|
|
info->sequence->chroma_width,
|
|
|
|
info->sequence->chroma_height);
|
|
|
|
mpeg2_skip (mpeg2dec, false);
|
2006-08-20 23:12:56 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
break;
|
|
|
|
case STATE_PICTURE:
|
|
|
|
break;
|
|
|
|
case STATE_SLICE:
|
|
|
|
case STATE_END:
|
|
|
|
case STATE_INVALID_END:
|
|
|
|
/* draw current picture */
|
2006-08-20 23:12:56 +00:00
|
|
|
if (info->display_fbuf) {
|
2007-03-22 09:18:46 +00:00
|
|
|
/* Wait if the audio thread is buffering - i.e. before
|
|
|
|
the first frames are decoded */
|
|
|
|
while (audiostatus == STREAM_BUFFERING) {
|
|
|
|
rb->sleep(1);
|
2006-08-20 23:12:56 +00:00
|
|
|
}
|
|
|
|
eta += (info->sequence->frame_period);
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
/* Convert eta (in 27MHz ticks) into audio samples */
|
|
|
|
eta2 =(eta * 44100) / 27000000;
|
|
|
|
s = samplesplayed - (rb->pcm_get_bytes_waiting() >> 2);
|
2006-08-20 23:12:56 +00:00
|
|
|
if (settings.limitfps) {
|
2007-03-22 09:18:46 +00:00
|
|
|
if (eta2 > s) {
|
|
|
|
rb->sleep(4); //((eta2-s)*HZ)/44100);
|
2006-08-20 23:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are more than 1/20 second behind schedule (and
|
|
|
|
more than 1/20 second into the decoding), skip frame */
|
2007-03-22 09:18:46 +00:00
|
|
|
if (settings.skipframes && (s > (44100/20)) &&
|
|
|
|
(eta2 < (s - (44100/20))) && (skipcount < 10)) {
|
2006-08-20 23:12:56 +00:00
|
|
|
skipped++;
|
2007-01-01 12:59:32 +00:00
|
|
|
skipcount++;
|
2006-08-20 23:12:56 +00:00
|
|
|
} else {
|
|
|
|
vo_draw_frame(info->display_fbuf->buf);
|
2007-01-01 12:59:32 +00:00
|
|
|
skipcount = 0;
|
2006-08-20 23:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate fps */
|
|
|
|
frame++;
|
|
|
|
if (settings.showfps && (*rb->current_tick-lasttick>=HZ)) {
|
2007-03-22 09:18:46 +00:00
|
|
|
fps=(frame*441000)/s;
|
2006-08-20 23:12:56 +00:00
|
|
|
rb->snprintf(str,sizeof(str),"%d.%d %d %d %d",
|
2007-03-22 09:18:46 +00:00
|
|
|
(fps/10),fps%10,skipped,s,eta2);
|
2006-08-20 23:12:56 +00:00
|
|
|
rb->lcd_putsxy(0,0,str);
|
|
|
|
rb->lcd_update_rect(0,0,LCD_WIDTH,8);
|
|
|
|
|
|
|
|
lasttick = *rb->current_tick;
|
|
|
|
}
|
|
|
|
}
|
2006-08-07 22:11:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb->yield();
|
|
|
|
}
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
done:
|
|
|
|
videostatus = STREAM_DONE;
|
2007-03-22 09:18:46 +00:00
|
|
|
/* Commit suicide */
|
|
|
|
rb->remove_thread(NULL);
|
2006-08-07 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
|
|
|
|
{
|
|
|
|
void* audiobuf;
|
|
|
|
int audiosize;
|
|
|
|
int in_file;
|
2006-08-09 10:47:22 +00:00
|
|
|
uint8_t* buffer;
|
|
|
|
size_t buffer_size;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* We define this here so it is on the main stack (in IRAM) */
|
|
|
|
mad_fixed_t mad_frame_overlap[2][32][18]; /* 4608 bytes */
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
/* This also stops audio playback - so we do it before using IRAM */
|
2006-11-26 18:31:41 +00:00
|
|
|
audiobuf = api->plugin_get_audio_buffer(&audiosize);
|
|
|
|
|
|
|
|
PLUGIN_IRAM_INIT(api)
|
|
|
|
rb = api;
|
2006-08-07 22:11:07 +00:00
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* Set disk pointers to NULL */
|
|
|
|
disk_buf_end = disk_buf = NULL;
|
|
|
|
|
|
|
|
/* Stream construction */
|
|
|
|
/* We take the first stream of each (audio and video) */
|
|
|
|
/* TODO : Search for these in the file first */
|
|
|
|
audio_str.curr_packet_end = audio_str.curr_packet = audio_str.next_packet = NULL;
|
|
|
|
video_str = audio_str;
|
|
|
|
video_str.id = 0xe0;
|
|
|
|
audio_str.id = 0xc0;
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
/* Initialise our malloc buffer */
|
|
|
|
mpeg2_alloc_init(audiobuf,audiosize);
|
|
|
|
|
2007-01-02 13:36:17 +00:00
|
|
|
/* Grab most of the buffer for the compressed video - leave some for
|
|
|
|
PCM audio data and some for libmpeg2 malloc use. */
|
2007-01-01 12:59:32 +00:00
|
|
|
buffer_size = audiosize - (PCMBUFFER_SIZE+AUDIOBUFFER_SIZE+LIBMPEG2BUFFER_SIZE);
|
2007-03-17 09:54:28 +00:00
|
|
|
DEBUGF("audiosize=%d, buffer_size=%ld\n",audiosize,buffer_size);
|
2007-01-01 12:59:32 +00:00
|
|
|
buffer = mpeg2_malloc(buffer_size,-1);
|
2006-08-09 10:47:22 +00:00
|
|
|
|
|
|
|
if (buffer == NULL)
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
mpa_buffer_size = AUDIOBUFFER_SIZE;
|
|
|
|
mpa_buffer = mpeg2_malloc(mpa_buffer_size,-2);
|
|
|
|
|
|
|
|
if (mpa_buffer == NULL)
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
|
|
|
|
pcm_buffer_size = PCMBUFFER_SIZE;
|
|
|
|
pcm_buffer = mpeg2_malloc(pcm_buffer_size,-2);
|
|
|
|
|
|
|
|
if (pcm_buffer == NULL)
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
|
|
|
|
/* The remaining buffer is for use by libmpeg2 */
|
2006-11-15 06:18:51 +00:00
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->lcd_set_backdrop(NULL);
|
2006-08-07 22:11:07 +00:00
|
|
|
rb->lcd_set_foreground(LCD_WHITE);
|
|
|
|
rb->lcd_set_background(LCD_BLACK);
|
|
|
|
#endif
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
rb->lcd_update();
|
|
|
|
|
|
|
|
if (parameter == NULL) {
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* Open the video file */
|
2006-08-07 22:11:07 +00:00
|
|
|
in_file = rb->open((char*)parameter,O_RDONLY);
|
|
|
|
|
|
|
|
if (in_file < 0) {
|
|
|
|
//fprintf(stderr,"Could not open %s\n",argv[1]);
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2006-08-20 23:12:56 +00:00
|
|
|
init_settings();
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->queue_init( &msg_queue, false ); /* Msg queue init */
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
/* make sure the backlight is always on when viewing video
|
|
|
|
(actually it should also set the timeout when plugged in,
|
|
|
|
but the function backlight_set_timeout_plugged is not
|
|
|
|
available in plugins) */
|
2007-02-18 10:07:27 +00:00
|
|
|
#if CONFIG_BACKLIGHT
|
2006-08-07 22:11:07 +00:00
|
|
|
if (rb->global_settings->backlight_timeout > 0)
|
|
|
|
rb->backlight_set_timeout(1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(true);
|
|
|
|
#endif
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
/* Initialise libmad */
|
|
|
|
rb->memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap));
|
|
|
|
init_mad(mad_frame_overlap);
|
|
|
|
|
|
|
|
eta = 0;
|
|
|
|
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(0, "Buffering...");
|
2007-01-01 12:59:32 +00:00
|
|
|
|
|
|
|
disk_buf_end = buffer + rb->read (in_file, buffer, buffer_size);
|
|
|
|
disk_buf = buffer;
|
|
|
|
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
|
|
|
|
2007-03-22 09:18:46 +00:00
|
|
|
audiostatus = STREAM_BUFFERING;
|
2007-01-01 12:59:32 +00:00
|
|
|
videostatus = STREAM_PLAYING;
|
|
|
|
|
|
|
|
/* We put the video thread on the second processor for multi-core targets. */
|
2007-03-22 09:18:46 +00:00
|
|
|
if ((videothread_id = rb->create_thread(video_thread,
|
2007-03-04 20:06:41 +00:00
|
|
|
(uint8_t*)video_stack,VIDEO_STACKSIZE,"mpgvideo" IF_PRIO(,PRIORITY_PLAYBACK)
|
2007-03-22 09:18:46 +00:00
|
|
|
IF_COP(, COP, true))) == NULL)
|
2007-01-01 12:59:32 +00:00
|
|
|
{
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ, "Cannot create video thread!");
|
2007-01-01 12:59:32 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
2007-03-22 09:18:46 +00:00
|
|
|
if ((audiothread_id = rb->create_thread(audio_thread,
|
2007-03-04 20:06:41 +00:00
|
|
|
(uint8_t*)audio_stack,AUDIO_STACKSIZE,"mpgaudio" IF_PRIO(,PRIORITY_PLAYBACK)
|
2007-03-22 09:18:46 +00:00
|
|
|
IF_COP(, CPU, false))) == NULL)
|
2007-01-01 12:59:32 +00:00
|
|
|
{
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ, "Cannot create audio thread!");
|
2007-03-22 09:18:46 +00:00
|
|
|
/* To do: Handle this error correctly on dual-core targets */
|
2007-01-02 13:36:17 +00:00
|
|
|
rb->remove_thread(videothread_id);
|
2007-01-01 12:59:32 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until both threads have finished their work */
|
|
|
|
while ((audiostatus != STREAM_DONE) || (videostatus != STREAM_DONE)) {
|
|
|
|
rb->sleep(HZ/10);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb->remove_thread(audiothread_id);
|
|
|
|
rb->yield(); /* Is this needed? */
|
|
|
|
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
rb->lcd_update();
|
2006-08-07 22:11:07 +00:00
|
|
|
|
|
|
|
mpeg2_close (mpeg2dec);
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->queue_delete( &msg_queue );
|
|
|
|
|
2006-08-07 22:11:07 +00:00
|
|
|
rb->close (in_file);
|
|
|
|
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(false);
|
|
|
|
#endif
|
|
|
|
|
2006-08-20 23:12:56 +00:00
|
|
|
save_settings(); /* Save settings (if they have changed) */
|
|
|
|
|
2007-01-01 12:59:32 +00:00
|
|
|
rb->lcd_setfont(FONT_UI);
|
|
|
|
|
2007-02-18 10:07:27 +00:00
|
|
|
#if CONFIG_BACKLIGHT
|
2006-08-07 22:11:07 +00:00
|
|
|
/* reset backlight settings */
|
|
|
|
rb->backlight_set_timeout(rb->global_settings->backlight_timeout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|