comparison src/madplug/decoder.c @ 2276:d25cd7e7eddb

append '\n' to format string for AUDDBG()
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Tue, 01 Jan 2008 23:12:46 +0900
parents 9a869d4bb0d3
children 2421c87fb855
comparison
equal deleted inserted replaced
2275:e09d9633d6aa 2276:d25cd7e7eddb
194 if(info->fileinfo_request == TRUE) { 194 if(info->fileinfo_request == TRUE) {
195 aud_tuple_associate_int(info->tuple, FIELD_LENGTH, NULL, -1); 195 aud_tuple_associate_int(info->tuple, FIELD_LENGTH, NULL, -1);
196 info->fileinfo_request = FALSE; 196 info->fileinfo_request = FALSE;
197 } 197 }
198 198
199 AUDDBG("f: scan_file"); 199 AUDDBG("f: scan_file\n");
200 AUDDBG("scan_file frames = %d", info->frames); 200 AUDDBG("scan_file frames = %d\n", info->frames);
201 201
202 while (1) { 202 while (1) {
203 remainder = stream.bufend - stream.next_frame; 203 remainder = stream.bufend - stream.next_frame;
204 204
205 /* 205 /*
212 memcpy(buffer, stream.this_frame, remainder); 212 memcpy(buffer, stream.this_frame, remainder);
213 len = input_get_data(info, buffer + remainder, 213 len = input_get_data(info, buffer + remainder,
214 BUFFER_SIZE - remainder); 214 BUFFER_SIZE - remainder);
215 215
216 if (len <= 0) { 216 if (len <= 0) {
217 AUDDBG("scan_file: len <= 0 len = %d", len); 217 AUDDBG("scan_file: len <= 0 len = %d\n", len);
218 break; 218 break;
219 } 219 }
220 220
221 mad_stream_buffer(&stream, buffer, len + remainder); 221 mad_stream_buffer(&stream, buffer, len + remainder);
222 222
224 if (mad_header_decode(&header, &stream) == -1) { 224 if (mad_header_decode(&header, &stream) == -1) {
225 if (stream.error == MAD_ERROR_BUFLEN) { 225 if (stream.error == MAD_ERROR_BUFLEN) {
226 break; 226 break;
227 } 227 }
228 if (!MAD_RECOVERABLE(stream.error)) { 228 if (!MAD_RECOVERABLE(stream.error)) {
229 AUDDBG("(fatal) error decoding header %d: %s", 229 AUDDBG("(fatal) error decoding header %d: %s\n",
230 info->frames, mad_stream_errorstr(&stream)); 230 info->frames, mad_stream_errorstr(&stream));
231 AUDDBG("remainder = %d", remainder); 231 AUDDBG("remainder = %d\n", remainder);
232 AUDDBG("len = %d", len); 232 AUDDBG("len = %d\n", len);
233 break; 233 break;
234 } 234 }
235 if (stream.error == MAD_ERROR_LOSTSYNC) { 235 if (stream.error == MAD_ERROR_LOSTSYNC) {
236 /* ignore LOSTSYNC due to ID3 tags */ 236 /* ignore LOSTSYNC due to ID3 tags */
237 tagsize = id3_tag_query(stream.this_frame, 237 tagsize = id3_tag_query(stream.this_frame,
238 stream.bufend - 238 stream.bufend -
239 stream.this_frame); 239 stream.this_frame);
240 if (tagsize > 0) { 240 if (tagsize > 0) {
241 AUDDBG("skipping id3_tag: %d", tagsize); 241 AUDDBG("skipping id3_tag: %d\n", tagsize);
242 mad_stream_skip(&stream, tagsize); 242 mad_stream_skip(&stream, tagsize);
243 continue; 243 continue;
244 } 244 }
245 } 245 }
246 246
247 AUDDBG("(recovered) error decoding header %d: %s", 247 AUDDBG("(recovered) error decoding header %d: %s\n",
248 info->frames, mad_stream_errorstr(&stream)); 248 info->frames, mad_stream_errorstr(&stream));
249 AUDDBG("remainder = %d", remainder); 249 AUDDBG("remainder = %d\n", remainder);
250 AUDDBG("len = %d", len); 250 AUDDBG("len = %d\n", len);
251 251
252 continue; 252 continue;
253 } 253 }
254 info->frames++; 254 info->frames++;
255 255
256 #ifdef DEBUG_INTENSIVELY 256 #ifdef DEBUG_INTENSIVELY
257 AUDDBG("header bitrate = %ld", header.bitrate); 257 AUDDBG("header bitrate = %ld\n", header.bitrate);
258 AUDDBG("duration = %ul", 258 AUDDBG("duration = %ul\n",
259 mad_timer_count(header.duration, 259 mad_timer_count(header.duration,
260 MAD_UNITS_MILLISECONDS)); 260 MAD_UNITS_MILLISECONDS));
261 AUDDBG("size = %d", stream.next_frame - stream.this_frame); 261 AUDDBG("size = %d\n", stream.next_frame - stream.this_frame);
262 #endif 262 #endif
263 if(aud_tuple_get_int(info->tuple, FIELD_LENGTH, NULL) == -1) 263 if(aud_tuple_get_int(info->tuple, FIELD_LENGTH, NULL) == -1)
264 mad_timer_add(&info->duration, header.duration); 264 mad_timer_add(&info->duration, header.duration);
265 else { 265 else {
266 gint length = aud_tuple_get_int(info->tuple, FIELD_LENGTH, NULL); 266 gint length = aud_tuple_get_int(info->tuple, FIELD_LENGTH, NULL);
278 info->mode = header.mode; 278 info->mode = header.mode;
279 279
280 if (audmad_config.use_xing) { 280 if (audmad_config.use_xing) {
281 frame.header = header; 281 frame.header = header;
282 if (mad_frame_decode(&frame, &stream) == -1) { 282 if (mad_frame_decode(&frame, &stream) == -1) {
283 AUDDBG("xing frame decode failed"); 283 AUDDBG("xing frame decode failed\n");
284 goto no_xing; 284 goto no_xing;
285 } 285 }
286 if (xing_parse(&info->xing, stream.anc_ptr, stream.anc_bitlen) == 0) { 286 if (xing_parse(&info->xing, stream.anc_ptr, stream.anc_bitlen) == 0) {
287 AUDDBG("xing header found "); 287 AUDDBG("xing header found\n");
288 has_xing = TRUE; 288 has_xing = TRUE;
289 info->vbr = TRUE; /* otherwise xing header would have been 'Info' */ 289 info->vbr = TRUE; /* otherwise xing header would have been 'Info' */
290 290
291 AUDDBG("xing: bytes = %ld frames = %ld", info->xing.bytes, info->xing.frames); 291 AUDDBG("xing: bytes = %ld frames = %ld\n", info->xing.bytes, info->xing.frames);
292 292
293 /* we have enough info to calculate bitrate and duration */ 293 /* we have enough info to calculate bitrate and duration */
294 if(info->xing.bytes && info->xing.frames) { 294 if(info->xing.bytes && info->xing.frames) {
295 xing_bitrate = 8 * (double)info->xing.bytes * 38 / (double)info->xing.frames; //38fps in MPEG1. 295 xing_bitrate = 8 * (double)info->xing.bytes * 38 / (double)info->xing.frames; //38fps in MPEG1.
296 #ifdef AUD_DEBUG 296 #ifdef AUD_DEBUG
297 { 297 {
298 gint tmp = (gint)(info->xing.bytes * 8 / xing_bitrate); 298 gint tmp = (gint)(info->xing.bytes * 8 / xing_bitrate);
299 AUDDBG("xing: bitrate = %4.1f kbps", xing_bitrate / 1000); 299 AUDDBG("xing: bitrate = %4.1f kbps\n", xing_bitrate / 1000);
300 AUDDBG("xing: duration = %d:%02d", tmp / 60, tmp % 60); 300 AUDDBG("xing: duration = %d:%02d\n", tmp / 60, tmp % 60);
301 } 301 }
302 #endif 302 #endif
303 } 303 }
304 continue; 304 continue;
305 } 305 }
306 #ifdef AUD_DEBUG 306 #ifdef AUD_DEBUG
307 else { 307 else {
308 AUDDBG("no usable xing header"); 308 AUDDBG("no usable xing header\n");
309 continue; 309 continue;
310 } 310 }
311 #endif 311 #endif
312 } /* xing */ 312 } /* xing */
313 313
330 } 330 }
331 no_xing: 331 no_xing:
332 if (fast && info->frames >= N_AVERAGE_FRAMES) { 332 if (fast && info->frames >= N_AVERAGE_FRAMES) {
333 float frame_size = ((double) data_used) / N_AVERAGE_FRAMES; 333 float frame_size = ((double) data_used) / N_AVERAGE_FRAMES;
334 334
335 AUDDBG("bitrate = %ld samplerate = %d", header.bitrate, header.samplerate); 335 AUDDBG("bitrate = %ld samplerate = %d\n", header.bitrate, header.samplerate);
336 AUDDBG("data_used = %d info->frames = %d info->size = %d tagsize = %d frame_size = %lf", 336 AUDDBG("data_used = %d info->frames = %d info->size = %d tagsize = %d frame_size = %lf\n",
337 data_used, info->frames, info->size, tagsize, frame_size); 337 data_used, info->frames, info->size, tagsize, frame_size);
338 338
339 if(info->size != 0) 339 if(info->size != 0)
340 info->frames = (info->size - tagsize) / frame_size; 340 info->frames = (info->size - tagsize) / frame_size;
341 341
342 AUDDBG("info->frames = %d", info->frames); 342 AUDDBG("info->frames = %d\n", info->frames);
343 343
344 if(aud_tuple_get_int(info->tuple, FIELD_LENGTH, NULL) == -1) { 344 if(aud_tuple_get_int(info->tuple, FIELD_LENGTH, NULL) == -1) {
345 if(xing_bitrate > 0.0) { 345 if(xing_bitrate > 0.0) {
346 /* calc duration with xing info */ 346 /* calc duration with xing info */
347 double tmp = 8 * (double)info->xing.bytes * 1000 / xing_bitrate; 347 double tmp = 8 * (double)info->xing.bytes * 1000 / xing_bitrate;
359 359
360 info->duration.seconds = length / 1000; 360 info->duration.seconds = length / 1000;
361 info->duration.fraction = length % 1000; 361 info->duration.fraction = length % 1000;
362 } 362 }
363 #ifdef AUD_DEBUG 363 #ifdef AUD_DEBUG
364 AUDDBG("using fast playtime calculation"); 364 AUDDBG("using fast playtime calculation\n");
365 AUDDBG("data used = %d [tagsize=%d framesize=%f]", 365 AUDDBG("data used = %d [tagsize=%d framesize=%f]\n",
366 data_used, tagsize, frame_size); 366 data_used, tagsize, frame_size);
367 AUDDBG("frames = %d, frequency = %d, channels = %d", 367 AUDDBG("frames = %d, frequency = %d, channels = %d\n",
368 info->frames, info->freq, info->channels); 368 info->frames, info->freq, info->channels);
369 long millis = mad_timer_count(info->duration, 369 long millis = mad_timer_count(info->duration,
370 MAD_UNITS_MILLISECONDS); 370 MAD_UNITS_MILLISECONDS);
371 AUDDBG("duration = %ld:%02ld", millis / 1000 / 60, (millis / 1000) % 60); 371 AUDDBG("duration = %ld:%02ld\n", millis / 1000 / 60, (millis / 1000) % 60);
372 #endif /* DEBUG */ 372 #endif /* DEBUG */
373 break; 373 break;
374 } 374 }
375 } /* while */ 375 } /* while */
376 if (stream.error != MAD_ERROR_BUFLEN) 376 if (stream.error != MAD_ERROR_BUFLEN)
390 mad_frame_finish(&frame); 390 mad_frame_finish(&frame);
391 mad_header_finish(&header); 391 mad_header_finish(&header);
392 mad_stream_finish(&stream); 392 mad_stream_finish(&stream);
393 xing_finish(&info->xing); 393 xing_finish(&info->xing);
394 394
395 AUDDBG("scan_file: info->frames = %d", info->frames); 395 AUDDBG("scan_file: info->frames = %d\n", info->frames);
396 AUDDBG("e: scan_file"); 396 AUDDBG("e: scan_file\n");
397 397
398 return (info->frames != 0 || info->remote == TRUE); 398 return (info->frames != 0 || info->remote == TRUE);
399 } 399 }
400 400
401 /* sanity check for audio open parameters */ 401 /* sanity check for audio open parameters */
426 struct mad_synth synth; 426 struct mad_synth synth;
427 427
428 /* track info is passed in as thread argument */ 428 /* track info is passed in as thread argument */
429 struct mad_info_t *info = (struct mad_info_t *) arg; 429 struct mad_info_t *info = (struct mad_info_t *) arg;
430 430
431 AUDDBG("f: decode"); 431 AUDDBG("f: decode\n");
432 432
433 /* init mad stuff */ 433 /* init mad stuff */
434 mad_frame_init(&frame); 434 mad_frame_init(&frame);
435 mad_stream_init(&stream); 435 mad_stream_init(&stream);
436 mad_stream_options(&stream, MAD_OPTION_IGNORECRC); 436 mad_stream_options(&stream, MAD_OPTION_IGNORECRC);
437 mad_synth_init(&synth); 437 mad_synth_init(&synth);
438 438
439 if(!info->playback){ 439 if(!info->playback){
440 AUDDBG("decode: playback == NULL"); 440 AUDDBG("decode: playback == NULL\n");
441 return NULL; 441 return NULL;
442 } 442 }
443 443
444 AUDDBG("decode: fmt = %d freq = %d channels = %d", info->fmt, info->freq, info->channels); 444 AUDDBG("decode: fmt = %d freq = %d channels = %d\n", info->fmt, info->freq, info->channels);
445 445
446 if(check_audio_param(info) == FALSE) 446 if(check_audio_param(info) == FALSE)
447 return NULL; 447 return NULL;
448 448
449 if (!info->playback->output->open_audio(info->fmt, info->freq, info->channels)) { 449 if (!info->playback->output->open_audio(info->fmt, info->freq, info->channels)) {
465 tlen = (gint) mad_timer_count(info->duration, MAD_UNITS_MILLISECONDS), 465 tlen = (gint) mad_timer_count(info->duration, MAD_UNITS_MILLISECONDS),
466 info->playback->set_params(info->playback, info->title, 466 info->playback->set_params(info->playback, info->title,
467 (tlen == 0 || info->size <= 0) ? -1 : tlen, 467 (tlen == 0 || info->size <= 0) ? -1 : tlen,
468 info->bitrate, info->freq, info->channels); 468 info->bitrate, info->freq, info->channels);
469 469
470 AUDDBG("decode: tlen = %d", tlen); 470 AUDDBG("decode: tlen = %d\n", tlen);
471 471
472 /* main loop */ 472 /* main loop */
473 do { 473 do {
474 if (!info->playback->playing) { 474 if (!info->playback->playing) {
475 AUDDBG("decode: stop signaled"); 475 AUDDBG("decode: stop signaled\n");
476 break; 476 break;
477 } 477 }
478 if (seek_skip) 478 if (seek_skip)
479 remainder = 0; 479 remainder = 0;
480 else { 480 else {
485 BUFFER_SIZE - remainder); 485 BUFFER_SIZE - remainder);
486 486
487 input_process_remote_metadata(info); 487 input_process_remote_metadata(info);
488 488
489 if (len <= 0) { 489 if (len <= 0) {
490 AUDDBG("finished decoding"); 490 AUDDBG("finished decoding\n");
491 break; 491 break;
492 } 492 }
493 len += remainder; 493 len += remainder;
494 if (len < MAD_BUFFER_GUARD) { 494 if (len < MAD_BUFFER_GUARD) {
495 int i; 495 int i;
500 500
501 mad_stream_buffer(&stream, buffer, len); 501 mad_stream_buffer(&stream, buffer, len);
502 502
503 if (seek_skip) { 503 if (seek_skip) {
504 504
505 AUDDBG("skipping: %d", seek_skip); 505 AUDDBG("skipping: %d\n", seek_skip);
506 506
507 int skip = 2; 507 int skip = 2;
508 do { 508 do {
509 if (mad_frame_decode(&frame, &stream) == 0) { 509 if (mad_frame_decode(&frame, &stream) == 0) {
510 mad_timer_add(&info->pos, frame.header.duration); 510 mad_timer_add(&info->pos, frame.header.duration);
524 } 524 }
525 525
526 while (info->playback->playing) { 526 while (info->playback->playing) {
527 if (info->seek != -1 && info->size > 0) { 527 if (info->seek != -1 && info->size > 0) {
528 528
529 AUDDBG("seeking: %ld", info->seek); 529 AUDDBG("seeking: %ld\n", info->seek);
530 530
531 int new_position; 531 int new_position;
532 gulong milliseconds = 532 gulong milliseconds =
533 mad_timer_count(info->duration, MAD_UNITS_MILLISECONDS); 533 mad_timer_count(info->duration, MAD_UNITS_MILLISECONDS);
534 if (info->seek >= milliseconds) 534 if (info->seek >= milliseconds)
539 ((double) info->seek / (double) milliseconds) * info->size; 539 ((double) info->seek / (double) milliseconds) * info->size;
540 540
541 if(new_position < 0) 541 if(new_position < 0)
542 new_position = 0; 542 new_position = 0;
543 543
544 AUDDBG("seeking to: %d bytes", new_position); 544 AUDDBG("seeking to: %d bytes\n", new_position);
545 545
546 if (aud_vfs_fseek(info->infile, new_position, SEEK_SET) == -1) 546 if (aud_vfs_fseek(info->infile, new_position, SEEK_SET) == -1)
547 audmad_error("failed to seek to: %d", new_position); 547 audmad_error("failed to seek to: %d", new_position);
548 mad_frame_mute(&frame); 548 mad_frame_mute(&frame);
549 mad_synth_mute(&synth); 549 mad_synth_mute(&synth);
568 mad_stream_skip(&stream, tagsize); 568 mad_stream_skip(&stream, tagsize);
569 continue; 569 continue;
570 } 570 }
571 } 571 }
572 572
573 AUDDBG("(recovered) error decoding header %d: %s", 573 AUDDBG("(recovered) error decoding header %d: %s\n",
574 info->current_frame, 574 info->current_frame,
575 mad_stream_errorstr(&stream)); 575 mad_stream_errorstr(&stream));
576 576
577 continue; 577 continue;
578 } 578 }
580 info->bitrate = frame.header.bitrate; 580 info->bitrate = frame.header.bitrate;
581 581
582 if (!audmad_config.show_avg_vbr_bitrate && info->vbr && (iteration % 40 == 0)) { 582 if (!audmad_config.show_avg_vbr_bitrate && info->vbr && (iteration % 40 == 0)) {
583 583
584 #ifdef DEBUG_INTENSIVELY 584 #ifdef DEBUG_INTENSIVELY
585 AUDDBG("decode vbr tlen = %d", tlen); 585 AUDDBG("decode vbr tlen = %d\n", tlen);
586 #endif 586 #endif
587 info->playback->set_params(info->playback, info->title, 587 info->playback->set_params(info->playback, info->title,
588 (tlen == 0 || info->size <= 0) ? -1 : tlen, 588 (tlen == 0 || info->size <= 0) ? -1 : tlen,
589 info->bitrate, info->freq, info->channels); 589 info->bitrate, info->freq, info->channels);
590 } 590 }
592 592
593 if (mad_frame_decode(&frame, &stream) == -1) { 593 if (mad_frame_decode(&frame, &stream) == -1) {
594 if (!MAD_RECOVERABLE(stream.error)) 594 if (!MAD_RECOVERABLE(stream.error))
595 break; 595 break;
596 596
597 AUDDBG("(recovered) error decoding frame %d: %s", 597 AUDDBG("(recovered) error decoding frame %d: %s\n",
598 info->current_frame, 598 info->current_frame,
599 mad_stream_errorstr(&stream)); 599 mad_stream_errorstr(&stream));
600 600
601 } 601 }
602 602
604 604
605 if (info->freq != frame.header.samplerate 605 if (info->freq != frame.header.samplerate
606 || info->channels != 606 || info->channels !=
607 (guint) MAD_NCHANNELS(&frame.header)) { 607 (guint) MAD_NCHANNELS(&frame.header)) {
608 608
609 AUDDBG("change in audio type detected"); 609 AUDDBG("change in audio type detected\n");
610 AUDDBG("old: frequency = %d, channels = %d", info->freq, 610 AUDDBG("old: frequency = %d, channels = %d\n", info->freq,
611 info->channels); 611 info->channels);
612 AUDDBG("new: frequency = %d, channels = %d", 612 AUDDBG("new: frequency = %d, channels = %d\n",
613 frame.header.samplerate, 613 frame.header.samplerate,
614 (guint) MAD_NCHANNELS(&frame.header)); 614 (guint) MAD_NCHANNELS(&frame.header));
615 615
616 info->freq = frame.header.samplerate; 616 info->freq = frame.header.samplerate;
617 info->channels = MAD_NCHANNELS(&frame.header); 617 info->channels = MAD_NCHANNELS(&frame.header);
618 618
619 if(audmad_config.force_reopen_audio && check_audio_param(info)) { 619 if(audmad_config.force_reopen_audio && check_audio_param(info)) {
620 gint current_time = info->playback->output->output_time(); 620 gint current_time = info->playback->output->output_time();
621 621
622 AUDDBG("re-opening audio due to change in audio type"); 622 AUDDBG("re-opening audio due to change in audio type\n");
623 623
624 info->playback->output->close_audio(); 624 info->playback->output->close_audio();
625 if (!info->playback->output->open_audio(info->fmt, info->freq, 625 if (!info->playback->output->open_audio(info->fmt, info->freq,
626 info->channels)) { 626 info->channels)) {
627 g_mutex_lock(pb_mutex); 627 g_mutex_lock(pb_mutex);
658 658
659 info->playback->output->buffer_free(); 659 info->playback->output->buffer_free();
660 info->playback->output->buffer_free(); 660 info->playback->output->buffer_free();
661 while (info->playback->output->buffer_playing()) { 661 while (info->playback->output->buffer_playing()) {
662 662
663 AUDDBG("f: buffer_playing=%d", info->playback->output->buffer_playing()); 663 AUDDBG("f: buffer_playing=%d\n", info->playback->output->buffer_playing());
664 664
665 g_get_current_time(&sleeptime); 665 g_get_current_time(&sleeptime);
666 g_time_val_add(&sleeptime, 500000); 666 g_time_val_add(&sleeptime, 500000);
667 667
668 g_mutex_lock(mad_mutex); 668 g_mutex_lock(mad_mutex);
673 } 673 }
674 674
675 } 675 }
676 } 676 }
677 677
678 AUDDBG("e: decode"); 678 AUDDBG("e: decode\n");
679 679
680 aud_tuple_free(info->tuple); 680 aud_tuple_free(info->tuple);
681 info->tuple = NULL; 681 info->tuple = NULL;
682 682
683 info->playback->output->close_audio(); 683 info->playback->output->close_audio();