summaryrefslogtreecommitdiff
path: root/apps/codecs/libspeex/modes.c
blob: 0478f978d1e3cc0e22b7008f491dc9708836ac47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* Copyright (C) 2002-2006 Jean-Marc Valin 
   File: modes.c

   Describes the different modes of the codec

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifdef HAVE_CONFIG_H
#include "config-speex.h"
#endif

#include "modes.h"
#include "ltp.h"
#include "quant_lsp.h"
#include "cb_search.h"
#include "sb_celp.h"
#include "nb_celp.h"
#include "vbr.h"
#include "misc.h"
#include <math.h>

#ifndef NULL
#define NULL 0
#endif


/* Extern declarations for all codebooks we use here */
extern const signed char gain_cdbk_nb[];
extern const signed char gain_cdbk_lbr[];
extern const signed char exc_5_256_table[];
extern const signed char exc_5_64_table[];
extern const signed char exc_8_128_table[];
extern const signed char exc_10_32_table[];
extern const signed char exc_10_16_table[];
extern const signed char exc_20_32_table[];


/* Parameters for Long-Term Prediction (LTP)*/
static const ltp_params ltp_params_nb = {
   gain_cdbk_nb,
   7,
   7
};

/* Parameters for Long-Term Prediction (LTP)*/
static const ltp_params ltp_params_vlbr = {
   gain_cdbk_lbr,
   5,
   0
};

/* Parameters for Long-Term Prediction (LTP)*/
static const ltp_params ltp_params_lbr = {
   gain_cdbk_lbr,
   5,
   7
};

/* Parameters for Long-Term Prediction (LTP)*/
static const ltp_params ltp_params_med = {
   gain_cdbk_lbr,
   5,
   7
};

/* Split-VQ innovation parameters for very low bit-rate narrowband */
static const split_cb_params split_cb_nb_vlbr = {
   10,               /*subvect_size*/
   4,               /*nb_subvect*/
   exc_10_16_table, /*shape_cb*/
   4,               /*shape_bits*/
   0,
};

/* Split-VQ innovation parameters for very low bit-rate narrowband */
static const split_cb_params split_cb_nb_ulbr = {
   20,               /*subvect_size*/
   2,               /*nb_subvect*/
   exc_20_32_table, /*shape_cb*/
   5,               /*shape_bits*/
   0,
};

/* Split-VQ innovation parameters for low bit-rate narrowband */
static const split_cb_params split_cb_nb_lbr = {
   10,              /*subvect_size*/
   4,               /*nb_subvect*/
   exc_10_32_table, /*shape_cb*/
   5,               /*shape_bits*/
   0,
};


/* Split-VQ innovation parameters narrowband */
static const split_cb_params split_cb_nb = {
   5,               /*subvect_size*/
   8,               /*nb_subvect*/
   exc_5_64_table, /*shape_cb*/
   6,               /*shape_bits*/
   0,
};

/* Split-VQ innovation parameters narrowband */
static const split_cb_params split_cb_nb_med = {
   8,               /*subvect_size*/
   5,               /*nb_subvect*/
   exc_8_128_table, /*shape_cb*/
   7,               /*shape_bits*/
   0,
};

/* Split-VQ innovation for low-band wideband */
static const split_cb_params split_cb_sb = {
   5,               /*subvect_size*/
   8,              /*nb_subvect*/
   exc_5_256_table,    /*shape_cb*/
   8,               /*shape_bits*/
   0,
};



/* 2150 bps "vocoder-like" mode for comfort noise */
static const SpeexSubmode nb_submode1 = {
   0,
   1,
   0,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /* LSP quantization */
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /* No pitch quantization */
   forced_pitch_quant,
   forced_pitch_unquant,
   NULL,
   /* No innovation quantization (noise only) */
   noise_codebook_quant,
   noise_codebook_unquant,
#else
   /* LSP quantization */
   NULL,
   lsp_unquant_lbr,
   /* No pitch quantization */
   NULL,
   forced_pitch_unquant,
   NULL,
   /* No innovation quantization (noise only) */
   NULL,
   noise_codebook_unquant,
#endif
   NULL,
   -1,
   43
};

/* 3.95 kbps very low bit-rate mode */
static const SpeexSubmode nb_submode8 = {
   0,
   1,
   0,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /*No pitch quantization*/
   forced_pitch_quant,
   forced_pitch_unquant,
   NULL,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb_ulbr,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_lbr,
   /*No pitch quantization*/
   NULL,
   forced_pitch_unquant,
   NULL,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_nb_ulbr,
#endif
   QCONST16(.5,15),
   79
};

/* 5.95 kbps very low bit-rate mode */
static const SpeexSubmode nb_submode2 = {
   0,
   0,
   0,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /*No pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_vlbr,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb_vlbr,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_lbr,
   /*No pitch quantization*/
   NULL,
   pitch_unquant_3tap,
   &ltp_params_vlbr,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_nb_vlbr,
#endif
   QCONST16(.6,15),
   119
};

/* 8 kbps low bit-rate mode */
static const SpeexSubmode nb_submode3 = {
   -1,
   0,
   1,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_lbr,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb_lbr,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_lbr,
   /*Pitch quantization*/
   NULL,
   pitch_unquant_3tap,
   &ltp_params_lbr,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_nb_lbr,
#endif
   QCONST16(.55,15),
   160
};

/* 11 kbps medium bit-rate mode */
static const SpeexSubmode nb_submode4 = {
   -1,
   0,
   1,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_med,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb_med,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_lbr,
   /*Pitch quantization*/
   NULL,
   pitch_unquant_3tap,
   &ltp_params_med,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_nb_med,
#endif
   QCONST16(.45,15),
   220
};

/* 15 kbps high bit-rate mode */
static const SpeexSubmode nb_submode5 = {
   -1,
   0,
   3,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_nb,
   lsp_unquant_nb,
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_nb,
   /*Pitch quantization*/
   NULL,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_nb,
#endif
   QCONST16(.3,15),
   300
};

/* 18.2 high bit-rate mode */
static const SpeexSubmode nb_submode6 = {
   -1,
   0,
   3,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_nb,
   lsp_unquant_nb,
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_sb,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_nb,
   /*Pitch quantization*/
   NULL,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_sb,
#endif
   QCONST16(.2,15),
   364
};

/* 24.6 kbps high bit-rate mode */
static const SpeexSubmode nb_submode7 = {
   -1,
   0,
   3,
   1,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_nb,
   lsp_unquant_nb,
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_nb,
   /*Pitch quantization*/
   NULL,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_nb,
#endif
   QCONST16(.1,15),
   492
};


/* Default mode for narrowband */
static const SpeexNBMode nb_mode = {
   160,    /*frameSize*/
   40,     /*subframeSize*/
   10,     /*lpcSize*/
   17,     /*pitchStart*/
   144,    /*pitchEnd*/
#ifdef FIXED_POINT
   29491, 19661, /* gamma1, gamma2 */
#else
   0.9, 0.6, /* gamma1, gamma2 */
#endif
   .012,   /*lag_factor*/
   QCONST16(.0002,15), /*lpc_floor*/
#ifdef EPIC_48K
   0,
#endif
   {NULL, &nb_submode1, &nb_submode2, &nb_submode3, &nb_submode4, &nb_submode5, &nb_submode6, &nb_submode7,
   &nb_submode8, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
   5,
   {1, 8, 2, 3, 3, 4, 4, 5, 5, 6, 7}
};


/* Default mode for narrowband */
const SpeexMode speex_nb_mode = {
   &nb_mode,
   nb_mode_query,
   "narrowband",
   0,
   4,
#ifndef SPEEX_DISABLE_ENCODER
   &nb_encoder_init,
   &nb_encoder_destroy,
   &nb_encode,
   &nb_decoder_init,
   &nb_decoder_destroy,
   &nb_decode,
   &nb_encoder_ctl,
   &nb_decoder_ctl,
#else
   NULL,
   NULL,
   NULL,
   &nb_decoder_init,
   &nb_decoder_destroy,
   &nb_decode,
   NULL,
   &nb_decoder_ctl,
#endif
};


/* Wideband part */


#ifdef EPIC_48K

extern const signed char gain_cdbk_ulbr[];
extern const signed char exc_12_32_table[];

/* Parameters for Long-Term Prediction (LTP)*/
static const ltp_params ltp_params_48k = {
   gain_cdbk_ulbr,
   3,
   0
};

static const split_cb_params split_cb_nb_48k = {
   12,               /*subvect_size*/
   4,               /*nb_subvect*/
   exc_12_32_table, /*shape_cb*/
   5,               /*shape_bits*/
   0,
};


/* 4.8 kbps very low bit-rate mode */
static const SpeexSubmode nb_48k_submode = {
   0,
   0,
   0,
   0,
#ifndef SPEEX_DISABLE_ENCODER
   /*LSP quantization*/
   lsp_quant_48k,
   lsp_unquant_48k,
   /*No pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_48k,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb_48k,
#else
   /*LSP quantization*/
   NULL,
   lsp_unquant_48k,
   /*No pitch quantization*/
   NULL,
   pitch_unquant_3tap,
   &ltp_params_48k,
   /*Innovation quantization*/
   NULL,
   split_cb_shape_sign_unquant,
   &split_cb_nb_48k,
#endif
   QCONST16(.7,15),
   144
};


/* Special, non-standard 4.8 kbps mode */
static const SpeexNBMode nb_48k_mode = {
   240,    /*frameSize*/
   48,     /*subframeSize*/
   10,     /*lpcSize*/
   17,     /*pitchStart*/
   144,    /*pitchEnd*/
   0.9,    /*gamma1*/
   0.6,    /*gamma2*/
   .01,   /*lag_factor*/
   QCONST16(.0003,15), /*lpc_floor*/
   1,
   {NULL, NULL, &nb_48k_submode, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
   2,
   {2,2,2,2,2,2,2,2,2,2,2}
};


/* Default mode for narrowband */
const SpeexMode speex_nb_48k_mode = {
   &nb_48k_mode,
   nb_mode_query,
   "narrowband 4.8 kbps",
   1000,
   4,
#ifndef SPEEX_DISABLE_ENCODER
   &nb_encoder_init,
   &nb_encoder_destroy,
   &nb_encode,
   &nb_decoder_init,
   &nb_decoder_destroy,
   &nb_decode,
   &nb_encoder_ctl,
   &nb_decoder_ctl,
#else
   NULL,
   NULL,
   NULL,
   &nb_decoder_init,
   &nb_decoder_destroy,
   &nb_decode,
   NULL,
   &nb_decoder_ctl,
#endif
};


#endif

int speex_mode_query(const SpeexMode *mode, int request, void *ptr)
{
   return mode->query(mode->mode, request, ptr);
}

class="hl kwd">pcm_output_flush(); /* Set the master clock */ set_stream_clock(time); /* Make sure streams are back in active pool */ move_strl_to_actl(); /* Prepare the parser and associated streams */ parser_prepare_streaming(); } /* Start buffer which optional force fill */ disk_buf_send_msg(STREAM_PLAY, fill_buffer); /* Tell each stream to start - may generate end of stream signals * now - we'll handle this when finished */ actl_stream_broadcast(STREAM_PLAY, 0); /* Actually start the clock */ pcm_output_play_pause(true); } /* Return the play time relative to the specified play time */ static uint32_t time_from_whence(uint32_t time, int whence) { int64_t currtime; uint32_t start; switch (whence) { case SEEK_SET: /* Set the current time (time = unsigned offset from 0) */ if (time > str_parser.duration) time = str_parser.duration; break; case SEEK_CUR: /* Seek forward or backward from the current time * (time = signed offset from current) */ currtime = stream_get_seek_time(&start); currtime -= start; currtime += (int32_t)time; if (currtime < 0) currtime = 0; else if ((uint64_t)currtime > str_parser.duration) currtime = str_parser.duration; time = (uint32_t)currtime; break; case SEEK_END: /* Seek from the end (time = unsigned offset from end) */ if (time > str_parser.duration) time = str_parser.duration; time = str_parser.duration - time; break; } return time; } /* Handle seeking details if playing or paused */ static uint32_t stream_seek_intl(uint32_t time, int whence, int status, bool *was_buffering) { if (status != STREAM_STOPPED) { bool wb; /* Place streams in a non-running state - keep them on actl */ actl_stream_broadcast(STREAM_STOP, 0); /* Stop all buffering or else risk clobbering random-access data */ wb = disk_buf_send_msg(STREAM_STOP, 0); if (was_buffering != NULL) *was_buffering = wb; } time = time_from_whence(time, whence); stream_mgr.seeked = true; return parser_seek_time(time); } /* Store the resume time at the last seek/current clock point */ static void stream_remember_resume_time(void) { /* Assume invalidity */ stream_mgr.resume_time = 0; if (stream_can_seek()) { /* Read the current stream time or the last seeked position */ uint32_t start; uint32_t time = stream_get_seek_time(&start); if (time >= str_parser.start_pts && time <= str_parser.end_pts) { /* Save the current stream time */ stream_mgr.resume_time = time - start; } } } /* Handle STREAM_OPEN */ void stream_on_open(const char *filename) { int err = STREAM_ERROR; stream_mgr_lock(); trigger_cpu_boost(); /* Open the video file */ if (disk_buf_open(filename) >= 0) { /* Initialize the parser */ err = parser_init_stream(); if (err >= STREAM_OK) { /* File ok - save the opened filename */ stream_mgr.filename = filename; } } /* If error - cleanup */ if (err < STREAM_OK) stream_on_close(); cancel_cpu_boost(); stream_mgr_unlock(); stream_mgr_reply_msg(err); } /* Handler STREAM_PLAY */ static void stream_on_play(void) { int status = stream_mgr.status; stream_mgr_lock(); if (status == STREAM_STOPPED) { uint32_t start; /* We just say we're playing now */ stream_mgr.status = STREAM_PLAYING; /* Reply with previous state */ stream_mgr_reply_msg(status); trigger_cpu_boost(); /* Seek to initial position and set clock to that time */ /* Save the resume time */ stream_remember_resume_time(); /* Prepare seek to start point */ start = stream_seek_intl(stream_mgr.resume_time, SEEK_SET, STREAM_STOPPED, NULL); /* Sync and start - force buffer fill */ stream_start_playback(start, true); } else { /* Reply with previous state */ stream_mgr_reply_msg(status); } stream_mgr_unlock(); } /* Handle STREAM_PAUSE */ static void stream_on_pause(void) { int status = stream_mgr.status; stream_mgr_lock(); /* Reply with previous state */ stream_mgr_reply_msg(status); if (status == STREAM_PLAYING) { /* Pause the clock */ pcm_output_play_pause(false); /* Pause each active stream */ actl_stream_broadcast(STREAM_PAUSE, 0); /* Pause the disk buffer - buffer may continue filling */ disk_buf_send_msg(STREAM_PAUSE, false); /* Unboost the CPU */ cancel_cpu_boost(); /* Offically paused */ stream_mgr.status = STREAM_PAUSED; } stream_mgr_unlock(); } /* Handle STREAM_RESUME */ static void stream_on_resume(void) { int status = stream_mgr.status; stream_mgr_lock(); /* Reply with previous state */ stream_mgr_reply_msg(status); if (status == STREAM_PAUSED) { /* Boost the CPU */ trigger_cpu_boost(); /* Sync and start - no force buffering */ stream_start_playback(str_parser.last_seek_time, false); /* Officially playing */ stream_mgr.status = STREAM_PLAYING; } stream_mgr_unlock(); } /* Handle STREAM_STOP */ static void stream_on_stop(bool reply) { int status = stream_mgr.status; stream_mgr_lock(); if (reply) stream_mgr_reply_msg(status); if (status != STREAM_STOPPED) { /* Pause the clock */ pcm_output_play_pause(false); /* Update the resume time info */ stream_remember_resume_time(); /* Not stopped = paused or playing */ stream_mgr.seeked = false; /* Stop buffering */ disk_buf_send_msg(STREAM_STOP, 0); /* Clear any still-active streams and remove from actl */ actl_stream_broadcast(STREAM_STOP, 1); /* Stop PCM output (and clock) */ pcm_output_stop(); /* Cancel our processor boost */ cancel_cpu_boost(); stream_mgr.status = STREAM_STOPPED; } stream_mgr_unlock(); } /* Handle STREAM_SEEK */ static void stream_on_seek(struct stream_seek_data *skd) { uint32_t time = skd->time; int whence = skd->whence; switch (whence) { case SEEK_SET: case SEEK_CUR: case SEEK_END: if (stream_mgr.filename == NULL) break; /* Keep things spinning if already doing so */ stream_keep_disk_active(); /* Have data - reply in order to acquire lock */ stream_mgr_reply_msg(STREAM_OK); stream_mgr_lock(); if (stream_can_seek()) { bool buffer; if (stream_mgr.status == STREAM_PLAYING) { /* Keep clock from advancing while seeking */ pcm_output_play_pause(false); } time = stream_seek_intl(time, whence, stream_mgr.status, &buffer); stream_remember_resume_time(); if (stream_mgr.status == STREAM_PLAYING) { /* Sync and restart - no force buffering */ stream_start_playback(time, buffer); } } stream_mgr_unlock(); return; } /* Invalid parameter or no file */ stream_mgr_reply_msg(STREAM_ERROR); } /* Handle STREAM_CLOSE */ static int stream_on_close(void) { int status = STREAM_STOPPED; stream_mgr_lock(); /* Any open file? */ if (stream_mgr.filename != NULL) { /* Yes - hide video */ stream_show_vo(false); /* Stop any playback */ status = stream_mgr.status; stream_on_stop(false); /* Tell parser file is finished */ parser_close_stream(); /* Close file */ disk_buf_close(); /* Reinitialize manager */ stream_mgr_init_state(); } stream_mgr_unlock(); return status; } /* Handle STREAM_EV_COMPLETE */ static void stream_on_ev_complete(struct stream *str) { stream_mgr_lock(); /* Stream is active? */ if (actl_stream_remove(str)) { /* No - remove this stream from the active list */ DEBUGF(" finished: 0x%02x\n", str->id); if (list_is_empty(&stream_mgr.actl)) { /* All streams have acked - stop playback */ stream_on_stop(false); stream_mgr.resume_time = 0; /* Played to end - no resume */ } else { /* Stream is done - stop it and place back in pool */ str_send_msg(str, STREAM_STOP, 1); } } stream_mgr_unlock(); } /* Callback for stream to notify about events internal to them */ void stream_generate_event(struct stream *str, long id, intptr_t data) { if (str == NULL) return; switch (id) { case STREAM_EV_COMPLETE: /* The last stream has ended */ stream_mgr_post_msg(STREAM_EV_COMPLETE, (intptr_t)str); break; } (void)data; } /* Clear any particular notification for which a stream registered */ void stream_clear_notify(struct stream *str, int for_msg) { switch (for_msg) { case DISK_BUF_DATA_NOTIFY: disk_buf_send_msg(DISK_BUF_CLEAR_DATA_NOTIFY, (intptr_t)str); break; } } /* Show/hide the video output */ bool stream_show_vo(bool show) { bool vis; stream_mgr_lock(); vis = parser_send_video_msg(VIDEO_DISPLAY_SHOW, show); #ifndef HAVE_LCD_COLOR grey_show(show); #endif stream_mgr_unlock(); return vis; } /* Query the visibility of video output */ bool stream_vo_is_visible(void) { bool vis; stream_mgr_lock(); vis = parser_send_video_msg(VIDEO_DISPLAY_IS_VISIBLE, 0); stream_mgr_unlock(); return vis; } /* Return the video dimensions */ bool stream_vo_get_size(struct vo_ext *sz) { bool retval = false; stream_mgr_lock(); if (str_parser.dims.w > 0 && str_parser.dims.h > 0) { *sz = str_parser.dims; retval = true; } stream_mgr_unlock(); return retval; } void stream_vo_set_clip(const struct vo_rect *rc) { stream_mgr_lock(); if (rc) { stream_mgr.parms.rc = *rc; rc = &stream_mgr.parms.rc; } parser_send_video_msg(VIDEO_SET_CLIP_RECT, (intptr_t)rc); stream_mgr_unlock(); } #ifndef HAVE_LCD_COLOR /* Show/hide the gray video overlay (independently of vo visibility). */ void stream_gray_show(bool show) { stream_mgr_lock(); grey_show(show); stream_mgr_unlock(); } #endif /* !HAVE_LCD_COLOR */ /* Display a thumbnail at the last seek point */ bool stream_display_thumb(const struct vo_rect *rc) { bool retval; if (rc == NULL) return false; stream_mgr_lock(); stream_mgr.parms.rc = *rc; retval = parser_send_video_msg(VIDEO_PRINT_THUMBNAIL, (intptr_t)&stream_mgr.parms.rc); stream_mgr_unlock(); return retval; } bool stream_draw_frame(bool no_prepare) { bool retval; stream_mgr_lock(); retval = parser_send_video_msg(VIDEO_PRINT_FRAME, no_prepare); stream_mgr_unlock(); return retval; } /* Return the time playback should resume if interrupted */ uint32_t stream_get_resume_time(void) { uint32_t resume_time; /* A stop request is async and replies before setting this - must lock */ stream_mgr_lock(); resume_time = stream_mgr.resume_time; stream_mgr_unlock(); return resume_time; } uint32_t stream_get_seek_time(uint32_t *start) { uint32_t time; stream_mgr_lock(); if (stream_mgr.seeked) { time = str_parser.last_seek_time; } else { time = TICKS_TO_TS(pcm_output_get_clock()); /* Clock can be start early so keep in range */ if (time < str_parser.start_pts) time = str_parser.start_pts; } if (start != NULL) *start = str_parser.start_pts; stream_mgr_unlock(); return time; } /* Wait for a state transistion to complete */ void stream_wait_status(void) { stream_mgr_lock(); stream_mgr_unlock(); } /* Returns the smallest file window that includes all active streams' * windows */ static bool stream_get_window_callback(struct list_item *item, struct stream_window *sw) { struct stream *str = TYPE_FROM_MEMBER(struct stream, item, l); off_t swl = str->hdr.win_left; off_t swr = str->hdr.win_right; if (swl < sw->left) sw->left = swl; if (swr > sw->right) sw->right = swr; return true; } bool stream_get_window(struct stream_window *sw) { if (sw == NULL) return false; sw->left = LONG_MAX; sw->right = LONG_MIN; actl_lock(); list_enum_items(&stream_mgr.actl, (list_enum_callback_t)stream_get_window_callback, (intptr_t)sw); actl_unlock(); return sw->left <= sw->right; } /* Playback control thread */ static void stream_mgr_thread(void) { struct queue_event ev; while (1) { rb->queue_wait(stream_mgr.q, &ev); switch (ev.id) { case STREAM_OPEN: stream_on_open((const char *)ev.data); break; case STREAM_CLOSE: stream_on_close(); break; case STREAM_PLAY: stream_on_play(); break; case STREAM_PAUSE: if (ev.data) stream_on_resume(); else stream_on_pause(); break; case STREAM_STOP: stream_on_stop(true); break; case STREAM_SEEK: stream_on_seek((struct stream_seek_data *)ev.data); break; case STREAM_EV_COMPLETE: stream_on_ev_complete((struct stream *)ev.data); break; case STREAM_QUIT: if (stream_mgr.status != STREAM_STOPPED) stream_on_stop(false); return; } } } /* Stream command interface APIs */ /* Opens a new file */ int stream_open(const char *filename) { if (stream_mgr.thread != 0) return stream_mgr_send_msg(STREAM_OPEN, (intptr_t)filename); return STREAM_ERROR; } /* Plays the current file starting at time 'start' */ int stream_play(void) { if (stream_mgr.thread != 0) return stream_mgr_send_msg(STREAM_PLAY, 0); return STREAM_ERROR; } /* Pauses playback if playing */ int stream_pause(void) { if (stream_mgr.thread != 0) return stream_mgr_send_msg(STREAM_PAUSE, false); return STREAM_ERROR; } /* Resumes playback if paused */ int stream_resume(void) { if (stream_mgr.thread != 0) return stream_mgr_send_msg(STREAM_PAUSE, true); return STREAM_ERROR; } /* Stops playback if not stopped */ int stream_stop(void) { if (stream_mgr.thread != 0) return stream_mgr_send_msg(STREAM_STOP, 0); return STREAM_ERROR; } /* Seeks playback time to/by the specified time */ int stream_seek(uint32_t time, int whence) { int ret; if (stream_mgr.thread == 0) return STREAM_ERROR; stream_mgr_lock(); stream_mgr.parms.skd.time = time; stream_mgr.parms.skd.whence = whence; ret = stream_mgr_send_msg(STREAM_SEEK, (intptr_t)&stream_mgr.parms.skd); stream_mgr_unlock(); return ret; } /* Closes the current file */ int stream_close(void) { if (stream_mgr.thread != 0) return stream_mgr_send_msg(STREAM_CLOSE, 0); return STREAM_ERROR; } /* Initializes the playback engine */ int stream_init(void) { void *mem; size_t memsize; stream_mgr.status = STREAM_STOPPED; stream_mgr_init_state(); list_initialize(&stream_mgr.actl); /* Initialize our window to the outside world first */ rb->mutex_init(&stream_mgr.str_mtx); rb->mutex_init(&stream_mgr.actl_mtx); stream_mgr.q = &stream_mgr_queue; rb->queue_init(stream_mgr.q, false); /* sets audiosize and returns buffer pointer */ mem = rb->plugin_get_audio_buffer(&memsize); /* Initialize non-allocator blocks first */ #ifndef HAVE_LCD_COLOR long greysize; /* Greylib init handles all necessary cache alignment */ if (!grey_init(mem, memsize, GREY_BUFFERED|GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, &greysize)) { rb->splash(HZ, "greylib init failed!"); return STREAM_ERROR; } mem += greysize; memsize -= greysize; grey_clear_display(); #endif /* !HAVE_LCD_COLOR */ stream_mgr.thread = rb->create_thread(stream_mgr_thread, stream_mgr_thread_stack, sizeof(stream_mgr_thread_stack), 0, "mpgstream_mgr" IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU)); rb->queue_enable_queue_send(stream_mgr.q, &stream_mgr_queue_send, stream_mgr.thread); if (stream_mgr.thread == 0) { rb->splash(HZ, "Could not create stream manager thread!"); return STREAM_ERROR; } /* Wait for thread to initialize */ stream_mgr_send_msg(STREAM_NULL, 0); /* Initialise our malloc buffer */ if (!mpeg_alloc_init(mem, memsize)) { rb->splash(HZ, "Out of memory in stream_init"); } /* These inits use the allocator */ else if (!pcm_output_init()) { rb->splash(HZ, "Could not initialize PCM!"); } else if (!audio_thread_init()) { rb->splash(HZ, "Cannot create audio thread!"); } else if (!video_thread_init()) { rb->splash(HZ, "Cannot create video thread!"); }