| | 503 | |
|---|
| | 504 | /***************************************************************************** |
|---|
| | 505 | * encoder_sys_t: encoder descriptor |
|---|
| | 506 | *****************************************************************************/ |
|---|
| | 507 | #define MAX_FRAME_SIZE 2000 |
|---|
| | 508 | #define MAX_FRAME_BYTES 2000 |
|---|
| | 509 | |
|---|
| | 510 | struct encoder_sys_t |
|---|
| | 511 | { |
|---|
| | 512 | /* |
|---|
| | 513 | * Input properties |
|---|
| | 514 | */ |
|---|
| | 515 | int i_headers; |
|---|
| | 516 | |
|---|
| | 517 | char *p_buffer; |
|---|
| | 518 | char *p_buffer_out[MAX_FRAME_BYTES]; |
|---|
| | 519 | |
|---|
| | 520 | /* |
|---|
| | 521 | * Speex properties |
|---|
| | 522 | */ |
|---|
| | 523 | SpeexBits bits; |
|---|
| | 524 | SpeexHeader header; |
|---|
| | 525 | SpeexStereoState stereo; |
|---|
| | 526 | void *p_state; |
|---|
| | 527 | |
|---|
| | 528 | int i_frames_per_packet; |
|---|
| | 529 | int i_frames_in_packet; |
|---|
| | 530 | |
|---|
| | 531 | int i_frame_length; |
|---|
| | 532 | int i_samples_delay; |
|---|
| | 533 | int i_frame_size; |
|---|
| | 534 | |
|---|
| | 535 | /* |
|---|
| | 536 | * Common properties |
|---|
| | 537 | */ |
|---|
| | 538 | mtime_t i_pts; |
|---|
| | 539 | }; |
|---|
| | 540 | |
|---|
| | 541 | /***************************************************************************** |
|---|
| | 542 | * OpenEncoder: probe the encoder and return score |
|---|
| | 543 | *****************************************************************************/ |
|---|
| | 544 | static int OpenEncoder( vlc_object_t *p_this ) |
|---|
| | 545 | { |
|---|
| | 546 | encoder_t *p_enc = (encoder_t *)p_this; |
|---|
| | 547 | encoder_sys_t *p_sys; |
|---|
| | 548 | SpeexMode *p_speex_mode = &speex_nb_mode; |
|---|
| | 549 | int i_quality; |
|---|
| | 550 | |
|---|
| | 551 | if( p_enc->fmt_out.i_codec != VLC_FOURCC('s','p','x',' ') ) |
|---|
| | 552 | { |
|---|
| | 553 | return VLC_EGENERIC; |
|---|
| | 554 | } |
|---|
| | 555 | |
|---|
| | 556 | /* Allocate the memory needed to store the decoder's structure */ |
|---|
| | 557 | if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL ) |
|---|
| | 558 | { |
|---|
| | 559 | msg_Err( p_enc, "out of memory" ); |
|---|
| | 560 | return VLC_EGENERIC; |
|---|
| | 561 | } |
|---|
| | 562 | p_enc->p_sys = p_sys; |
|---|
| | 563 | p_enc->pf_header = Headers; |
|---|
| | 564 | p_enc->pf_encode_audio = Encode; |
|---|
| | 565 | p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE; |
|---|
| | 566 | |
|---|
| | 567 | speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate, |
|---|
| | 568 | 1, p_speex_mode ); |
|---|
| | 569 | |
|---|
| | 570 | p_sys->header.frames_per_packet = 1; |
|---|
| | 571 | p_sys->header.vbr = 1; |
|---|
| | 572 | p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels; |
|---|
| | 573 | |
|---|
| | 574 | /* Create a new encoder state in narrowband mode */ |
|---|
| | 575 | p_sys->p_state = speex_encoder_init( p_speex_mode ); |
|---|
| | 576 | |
|---|
| | 577 | /* Set the quality to 8 (15 kbps) */ |
|---|
| | 578 | i_quality = 8; |
|---|
| | 579 | speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_quality ); |
|---|
| | 580 | |
|---|
| | 581 | /*Initialization of the structure that holds the bits*/ |
|---|
| | 582 | speex_bits_init( &p_sys->bits ); |
|---|
| | 583 | |
|---|
| | 584 | p_sys->i_frames_in_packet = 0; |
|---|
| | 585 | p_sys->i_samples_delay = 0; |
|---|
| | 586 | p_sys->i_headers = 0; |
|---|
| | 587 | p_sys->i_pts = 0; |
|---|
| | 588 | |
|---|
| | 589 | speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE, |
|---|
| | 590 | &p_sys->i_frame_length ); |
|---|
| | 591 | |
|---|
| | 592 | p_sys->i_frame_size = p_sys->i_frame_length * |
|---|
| | 593 | sizeof(int16_t) * p_enc->fmt_in.audio.i_channels; |
|---|
| | 594 | p_sys->p_buffer = malloc( p_sys->i_frame_size ); |
|---|
| | 595 | |
|---|
| | 596 | msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d", |
|---|
| | 597 | p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels, |
|---|
| | 598 | p_enc->fmt_in.audio.i_rate ); |
|---|
| | 599 | |
|---|
| | 600 | return VLC_SUCCESS; |
|---|
| | 601 | } |
|---|
| | 602 | |
|---|
| | 603 | /**************************************************************************** |
|---|
| | 604 | * Headers: spits out the headers |
|---|
| | 605 | **************************************************************************** |
|---|
| | 606 | * This function spits out ogg packets. |
|---|
| | 607 | ****************************************************************************/ |
|---|
| | 608 | static block_t *Headers( encoder_t *p_enc ) |
|---|
| | 609 | { |
|---|
| | 610 | encoder_sys_t *p_sys = p_enc->p_sys; |
|---|
| | 611 | block_t *p_block, *p_chain = NULL; |
|---|
| | 612 | |
|---|
| | 613 | /* Create speex headers */ |
|---|
| | 614 | if( !p_sys->i_headers ) |
|---|
| | 615 | { |
|---|
| | 616 | char *p_buffer; |
|---|
| | 617 | int i_buffer; |
|---|
| | 618 | |
|---|
| | 619 | /* Main header */ |
|---|
| | 620 | p_buffer = speex_header_to_packet( &p_sys->header, &i_buffer ); |
|---|
| | 621 | p_block = block_New( p_enc, i_buffer ); |
|---|
| | 622 | memcpy( p_block->p_buffer, p_buffer, i_buffer ); |
|---|
| | 623 | p_block->i_dts = p_block->i_pts = p_block->i_length = 0; |
|---|
| | 624 | block_ChainAppend( &p_chain, p_block ); |
|---|
| | 625 | |
|---|
| | 626 | /* Comment */ |
|---|
| | 627 | p_block = block_New( p_enc, sizeof("ENCODER=VLC media player") ); |
|---|
| | 628 | memcpy( p_block->p_buffer, "ENCODER=VLC media player", |
|---|
| | 629 | p_block->i_buffer ); |
|---|
| | 630 | p_block->i_dts = p_block->i_pts = p_block->i_length = 0; |
|---|
| | 631 | block_ChainAppend( &p_chain, p_block ); |
|---|
| | 632 | |
|---|
| | 633 | p_sys->i_headers = 2; |
|---|
| | 634 | } |
|---|
| | 635 | |
|---|
| | 636 | return p_chain; |
|---|
| | 637 | } |
|---|
| | 638 | |
|---|
| | 639 | /**************************************************************************** |
|---|
| | 640 | * Encode: the whole thing |
|---|
| | 641 | **************************************************************************** |
|---|
| | 642 | * This function spits out ogg packets. |
|---|
| | 643 | ****************************************************************************/ |
|---|
| | 644 | static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf ) |
|---|
| | 645 | { |
|---|
| | 646 | encoder_sys_t *p_sys = p_enc->p_sys; |
|---|
| | 647 | block_t *p_block, *p_chain = NULL; |
|---|
| | 648 | |
|---|
| | 649 | char *p_buffer = p_aout_buf->p_buffer; |
|---|
| | 650 | int i_samples = p_aout_buf->i_nb_samples; |
|---|
| | 651 | int i_samples_delay = p_sys->i_samples_delay; |
|---|
| | 652 | |
|---|
| | 653 | p_sys->i_pts = p_aout_buf->start_date - |
|---|
| | 654 | (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay / |
|---|
| | 655 | (mtime_t)p_enc->fmt_in.audio.i_rate; |
|---|
| | 656 | |
|---|
| | 657 | p_sys->i_samples_delay += i_samples; |
|---|
| | 658 | |
|---|
| | 659 | while( p_sys->i_samples_delay >= p_sys->i_frame_length ) |
|---|
| | 660 | { |
|---|
| | 661 | int16_t *p_samples; |
|---|
| | 662 | int i_out; |
|---|
| | 663 | |
|---|
| | 664 | if( i_samples_delay ) |
|---|
| | 665 | { |
|---|
| | 666 | /* Take care of the left-over from last time */ |
|---|
| | 667 | int i_delay_size = i_samples_delay * 2 * |
|---|
| | 668 | p_enc->fmt_in.audio.i_channels; |
|---|
| | 669 | int i_size = p_sys->i_frame_size - i_delay_size; |
|---|
| | 670 | |
|---|
| | 671 | p_samples = (int16_t *)p_sys->p_buffer; |
|---|
| | 672 | memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size ); |
|---|
| | 673 | p_buffer -= i_delay_size; |
|---|
| | 674 | i_samples += i_samples_delay; |
|---|
| | 675 | i_samples_delay = 0; |
|---|
| | 676 | } |
|---|
| | 677 | else |
|---|
| | 678 | { |
|---|
| | 679 | p_samples = (int16_t *)p_buffer; |
|---|
| | 680 | } |
|---|
| | 681 | |
|---|
| | 682 | /* Encode current frame */ |
|---|
| | 683 | if( p_enc->fmt_in.audio.i_channels == 2 ) |
|---|
| | 684 | speex_encode_stereo( p_samples, p_sys->i_frame_length, |
|---|
| | 685 | &p_sys->bits ); |
|---|
| | 686 | |
|---|
| | 687 | #if 0 |
|---|
| | 688 | if( p_sys->preprocess ) |
|---|
| | 689 | speex_preprocess( p_sys->preprocess, p_samples, NULL ); |
|---|
| | 690 | #endif |
|---|
| | 691 | |
|---|
| | 692 | speex_encode( p_sys->p_state, p_samples, &p_sys->bits ); |
|---|
| | 693 | |
|---|
| | 694 | p_buffer += p_sys->i_frame_size; |
|---|
| | 695 | p_sys->i_samples_delay -= p_sys->i_frame_length; |
|---|
| | 696 | i_samples -= p_sys->i_frame_length; |
|---|
| | 697 | |
|---|
| | 698 | p_sys->i_frames_in_packet++; |
|---|
| | 699 | |
|---|
| | 700 | if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet ) |
|---|
| | 701 | continue; |
|---|
| | 702 | |
|---|
| | 703 | p_sys->i_frames_in_packet = 0; |
|---|
| | 704 | |
|---|
| | 705 | speex_bits_insert_terminator( &p_sys->bits ); |
|---|
| | 706 | i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out, |
|---|
| | 707 | MAX_FRAME_BYTES ); |
|---|
| | 708 | speex_bits_reset( &p_sys->bits ); |
|---|
| | 709 | |
|---|
| | 710 | p_block = block_New( p_enc, i_out ); |
|---|
| | 711 | memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out ); |
|---|
| | 712 | |
|---|
| | 713 | p_block->i_length = (mtime_t)1000000 * |
|---|
| | 714 | (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet / |
|---|
| | 715 | (mtime_t)p_enc->fmt_in.audio.i_rate; |
|---|
| | 716 | |
|---|
| | 717 | p_block->i_dts = p_block->i_pts = p_sys->i_pts; |
|---|
| | 718 | |
|---|
| | 719 | /* Update pts */ |
|---|
| | 720 | p_sys->i_pts += p_block->i_length; |
|---|
| | 721 | block_ChainAppend( &p_chain, p_block ); |
|---|
| | 722 | |
|---|
| | 723 | } |
|---|
| | 724 | |
|---|
| | 725 | /* Backup the remaining raw samples */ |
|---|
| | 726 | if( i_samples ) |
|---|
| | 727 | { |
|---|
| | 728 | memcpy( p_sys->p_buffer, p_buffer + i_samples_delay * 2 * |
|---|
| | 729 | p_enc->fmt_in.audio.i_channels, |
|---|
| | 730 | i_samples * 2 * p_enc->fmt_in.audio.i_channels ); |
|---|
| | 731 | } |
|---|
| | 732 | |
|---|
| | 733 | return p_chain; |
|---|
| | 734 | } |
|---|
| | 735 | |
|---|
| | 736 | /***************************************************************************** |
|---|
| | 737 | * CloseEncoder: encoder destruction |
|---|
| | 738 | *****************************************************************************/ |
|---|
| | 739 | static void CloseEncoder( vlc_object_t *p_this ) |
|---|
| | 740 | { |
|---|
| | 741 | encoder_t *p_enc = (encoder_t *)p_this; |
|---|
| | 742 | encoder_sys_t *p_sys = p_enc->p_sys; |
|---|
| | 743 | |
|---|
| | 744 | speex_encoder_destroy( p_sys->p_state ); |
|---|
| | 745 | speex_bits_destroy( &p_sys->bits ); |
|---|
| | 746 | |
|---|
| | 747 | if( p_sys->p_buffer ) free( p_sys->p_buffer ); |
|---|
| | 748 | free( p_sys ); |
|---|
| | 749 | } |
|---|