~ubuntu-branches/ubuntu/trusty/libdv/trusty

« back to all changes in this revision

Viewing changes to libdv/encode.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2004-07-19 12:19:44 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040719121944-17vuryc01yeyx8hf
Tags: 0.103-2
* debian/rules: Provide separate doc directory for libdv4-dev.
* debian/libdv4-dev.links: No longer symlink doc dir to the one
  from libdv4.
* debian/NEWS: Only install into libdv4-dev. Closes: #259694

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
 
1
 /*
2
2
 *  encode.c
3
3
 *
4
4
 *     Copyright (C) James Bowman  - July 2000
8
8
 *  codec.
9
9
 *
10
10
 *  libdv is free software; you can redistribute it and/or modify it
11
 
 *  under the terms of the GNU General Public License as published by
12
 
 *  the Free Software Foundation; either version 2, or (at your
 
11
 *  under the terms of the GNU Lesser Public License as published by
 
12
 *  the Free Software Foundation; either version 2.1, or (at your
13
13
 *  option) any later version.
14
14
 *   
15
15
 *  libdv is distributed in the hope that it will be useful, but
16
16
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
17
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 
 *  General Public License for more details.
 
18
 *  Lesser Public License for more details.
19
19
 *   
20
 
 *  You should have received a copy of the GNU General Public License
21
 
 *  along with GNU Make; see the file COPYING.  If not, write to
 
20
 *  You should have received a copy of the GNU Lesser Public License
 
21
 *  along with libdv; see the file COPYING.  If not, write to
22
22
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
23
23
 *
24
24
 *  The libdv homepage is http://libdv.sourceforge.net/.  
36
36
 *  @{
37
37
 */
38
38
 
 
39
#if HAVE_CONFIG_H
 
40
# include <config.h>
 
41
#endif
 
42
 
39
43
#include <time.h>
40
44
#include <math.h>
41
45
#include <string.h>
42
46
#include <stdio.h>
43
47
#include <stdlib.h>
44
48
#include <unistd.h>
45
 
#include <stdint.h>
 
49
#include <inttypes.h>
 
50
#include <pthread.h>
46
51
 
47
52
#include "dv.h"
48
53
#include "encode.h"
318
323
 
319
324
static dv_vlc_encode_t * vlc_test_lookup[512];
320
325
 
321
 
void init_vlc_test_lookup()
 
326
void _dv_init_vlc_test_lookup()
322
327
{
323
328
        int i;
324
329
        memset(vlc_test_lookup, 0, 512 * sizeof(dv_vlc_encode_t*));
407
412
        return ++o;
408
413
}
409
414
 
410
 
dv_vlc_entry_t * vlc_encode_lookup;
411
 
unsigned char  * vlc_num_bits_lookup;
 
415
dv_vlc_entry_t * vlc_encode_lookup = NULL;
 
416
unsigned char  * vlc_num_bits_lookup = NULL;
412
417
 
413
 
void init_vlc_encode_lookup(void)
 
418
void _dv_init_vlc_encode_lookup(void)
414
419
{
415
420
        int run,amp;
416
 
        vlc_encode_lookup = (dv_vlc_entry_t *) malloc(
417
 
                32768 * 2 * sizeof(dv_vlc_entry_t));
418
 
        vlc_num_bits_lookup = (unsigned char*) malloc(32768);
 
421
        
 
422
        if (vlc_encode_lookup == NULL)
 
423
                vlc_encode_lookup = (dv_vlc_entry_t *) malloc(
 
424
                        32768 * 2 * sizeof(dv_vlc_entry_t));
 
425
        if (vlc_num_bits_lookup == NULL)
 
426
                vlc_num_bits_lookup = (unsigned char*) malloc(32768);
419
427
                
420
428
        for (run = 0; run <= 63; run++) {
421
429
                for (amp = 0; amp <= 255; amp++) {
465
473
        14,28,30,44,46,58,60,64
466
474
};
467
475
 
468
 
void prepare_reorder_tables(void)
 
476
 
 
477
void _dv_prepare_reorder_tables(void)
469
478
{
470
479
        int i;
471
480
        for (i = 0; i < 64; i++) {
472
 
                reorder_88[i]--;
473
 
                reorder_88[i] *= 2;
 
481
                reorder_88[i]--;
 
482
                reorder_88[i] *= 2;
474
483
                reorder_248[i]--;
475
484
                reorder_248[i] *= 2;
476
485
        }
477
486
}
478
487
 
479
 
extern int reorder_block_mmx(dv_coeff_t * a, 
 
488
extern int _dv_reorder_block_mmx(dv_coeff_t * a, 
480
489
                             const unsigned short* reorder_table);
481
490
 
482
491
static void reorder_block(dv_block_t *bl)
493
502
                reorder = reorder_248;
494
503
 
495
504
#if ARCH_X86
496
 
        reorder_block_mmx(bl->coeffs, reorder);
 
505
        _dv_reorder_block_mmx(bl->coeffs, reorder);
497
506
        emms();
498
507
#else   
499
508
        for (i = 0; i < 64; i++) {
503
512
#endif
504
513
}
505
514
 
506
 
extern unsigned long vlc_encode_block_mmx(dv_coeff_t* coeffs,
 
515
extern unsigned long _dv_vlc_encode_block_mmx(dv_coeff_t* coeffs,
507
516
                                          dv_vlc_entry_t ** out);
508
517
 
509
518
static unsigned long vlc_encode_block(dv_coeff_t* coeffs, dv_vlc_block_t* out)
538
547
#else
539
548
        int num_bits;
540
549
 
541
 
        num_bits = vlc_encode_block_mmx(coeffs, &o);
 
550
        num_bits = _dv_vlc_encode_block_mmx(coeffs, &o);
542
551
        emms();
543
552
#endif
544
553
        *o++ = set_dv_vlc(0x6, 4); /* EOB */
549
558
        return num_bits;
550
559
}
551
560
 
552
 
extern unsigned long vlc_num_bits_block_x86(dv_coeff_t* coeffs);
 
561
extern unsigned long _dv_vlc_num_bits_block_x86(dv_coeff_t* coeffs);
553
562
 
554
 
extern unsigned long vlc_num_bits_block(dv_coeff_t* coeffs)
 
563
extern unsigned long _dv_vlc_num_bits_block(dv_coeff_t* coeffs)
555
564
{
556
565
#if !ARCH_X86
557
566
        dv_coeff_t * z = coeffs + 1; /* First AC coeff */
572
581
 
573
582
        return num_bits;
574
583
#else
575
 
        return vlc_num_bits_block_x86(coeffs);
 
584
        return _dv_vlc_num_bits_block_x86(coeffs);
576
585
#endif
577
586
}
578
587
 
619
628
        *e = set_dv_vlc(get_dv_vlc_val(*e) & ((1 << len)-1), len);
620
629
}          
621
630
 
622
 
extern void vlc_encode_block_pass_1_x86(dv_vlc_entry_t** start,
 
631
extern void _dv_vlc_encode_block_pass_1_x86(dv_vlc_entry_t** start,
623
632
                                        dv_vlc_entry_t*  end,
624
633
                                        long* bit_budget,
625
634
                                        long* bit_offset,
648
657
        bl->bit_budget = bit_budget;
649
658
        bl->bit_offset = bit_offset;
650
659
#else
651
 
        vlc_encode_block_pass_1_x86(&bl->coeffs_start, bl->coeffs_end,
 
660
        _dv_vlc_encode_block_pass_1_x86(&bl->coeffs_start, bl->coeffs_end,
652
661
                                    &bl->bit_budget, &bl->bit_offset,
653
662
                                    vsbuffer);
654
663
#endif
724
733
        return;
725
734
}
726
735
 
727
 
extern int classify_mmx(dv_coeff_t * a, unsigned short* amp_ofs,
 
736
extern int _dv_classify_mmx(dv_coeff_t * a, unsigned short* amp_ofs,
728
737
                        unsigned short* amp_cmp);
729
738
 
730
 
inline int classify(dv_coeff_t * bl)
 
739
static inline int classify(dv_coeff_t * bl)
731
740
{
732
741
#if ARCH_X86
733
742
        static unsigned short amp_ofs[3][4] = { 
745
754
        dc = bl[0];
746
755
        bl[0] = 0;
747
756
        for (i = 0; i < 3; i++) {
748
 
                if (classify_mmx(bl, amp_ofs[i], amp_cmp[i])) {
 
757
                if (_dv_classify_mmx(bl, amp_ofs[i], amp_cmp[i])) {
749
758
                        bl[0] = dc;
750
759
                        emms();
751
760
                        return 3-i;
792
801
                dv_block_t *bl = &mb->b[b];
793
802
                
794
803
                if (bl->dct_mode == DV_DCT_88) {
795
 
                        dct_88(bl->coeffs);
 
804
                        _dv_dct_88(bl->coeffs);
796
805
#if !ARCH_X86
797
806
                        reorder_block(bl);
798
807
#endif
799
808
#if BRUTE_FORCE_DCT_88
800
 
                        weight_88(bl->coeffs);
 
809
                        _dv_weight_88(bl->coeffs);
801
810
#endif
802
811
                } else {
803
 
                        dct_248(bl->coeffs);
 
812
                        _dv_dct_248(bl->coeffs);
804
813
                        reorder_block(bl);
805
814
#if BRUTE_FORCE_DCT_248
806
 
                        weight_248(bl->coeffs);
 
815
                        _dv_weight_248(bl->coeffs);
807
816
#endif
808
817
                }
809
818
                dct_used[bl->dct_mode]++;
831
840
static int qnos_class_combi[16][16];
832
841
static int qno_next_hit[4][16];
833
842
 
834
 
void init_qno_start(void)
 
843
void _dv_init_qno_start(void)
835
844
{
836
845
        int qno;
837
846
        int klass;
918
927
                        while (smallest_qno > 0) {
919
928
                                memcpy(bb[b], bl->coeffs, 
920
929
                                       64 *sizeof(dv_coeff_t));
921
 
                                quant(bb[b], smallest_qno, bl->class_no);
922
 
                                if (vlc_num_bits_block(bb[b]) 
 
930
                                _dv_quant(bb[b], smallest_qno, bl->class_no);
 
931
                                if (_dv_vlc_num_bits_block(bb[b]) 
923
932
                                    <= ac_coeff_budget)
924
933
                                        break;
925
934
                                qno_index++;
937
946
                if (smallest_qno != 15) { 
938
947
                        for (b = 0; b < 6; b++) {
939
948
                                dv_block_t *bl = &mb->b[b];
940
 
                                quant(bl->coeffs, smallest_qno, bl->class_no);
 
949
                                _dv_quant(bl->coeffs, smallest_qno, bl->class_no);
941
950
                                vlc_encode_block(bl->coeffs, vblocks + b);
942
951
                        }
943
952
                        
978
987
                        int bits;
979
988
                        dv_block_t *bl = &mb->b[b];
980
989
                        memcpy(bb[b], bl->coeffs, 64 *sizeof(dv_coeff_t));
981
 
                        quant(bb[b], qno, bl->class_no);
982
 
                        bits = vlc_num_bits_block(bb[b]);
 
990
                        _dv_quant(bb[b], qno, bl->class_no);
 
991
                        bits = _dv_vlc_num_bits_block(bb[b]);
983
992
                        bits_used += bits;
984
993
#if 0
985
994
                        bits_used_ += bits;
1012
1021
                                        dv_block_t *bl = &mb->b[b];
1013
1022
                                        memcpy(bb[b], bl->coeffs, 
1014
1023
                                               64 *sizeof(dv_coeff_t));
1015
 
                                        quant(bb[b], qno, bl->class_no);
1016
 
                                        bits_used += vlc_num_bits_block(bb[b]);
 
1024
                                        _dv_quant(bb[b], qno, bl->class_no);
 
1025
                                        bits_used += _dv_vlc_num_bits_block(bb[b]);
1017
1026
                                }
1018
1027
 
1019
1028
                                if (bits_used > ac_coeff_budget) {
1041
1050
                if (qno != 15) { 
1042
1051
                        for (b = 0; b < 6; b++) {
1043
1052
                                dv_block_t *bl = &mb->b[b];
1044
 
                                quant(bl->coeffs, qno, bl->class_no);
 
1053
                                _dv_quant(bl->coeffs, qno, bl->class_no);
1045
1054
                                vlc_encode_block(bl->coeffs, vblocks + b);
1046
1055
                        }
1047
1056
                        if (qno == 0 || static_qno) {
1082
1091
                for (b = 0; b < 6; b++) {
1083
1092
                        dv_block_t *bl = &mb->b[b];
1084
1093
                        memcpy(bb[m][b], bl->coeffs, 64 * sizeof(dv_coeff_t));
1085
 
                        quant(bb[m][b], smallest_qno[m], bl->class_no);
1086
 
                        bits_used[m] += vlc_num_bits_block(bb[m][b]);
 
1094
                        _dv_quant(bb[m][b], smallest_qno[m], bl->class_no);
 
1095
                        bits_used[m] += _dv_vlc_num_bits_block(bb[m][b]);
1087
1096
                        class_combi[m] |= (1 << bl->class_no);
1088
1097
                }
1089
1098
                while (qnos_class_combi[class_combi[m]][qno_index[m]] > 15) {
1145
1154
                for (b = 0; b < 6; b++) {
1146
1155
                        dv_block_t *bl = &mb->b[b];
1147
1156
                        memcpy(bb[m][b], bl->coeffs, 64 *sizeof(dv_coeff_t));
1148
 
                        quant(bb[m][b], smallest_qno[m], bl->class_no);
1149
 
                        bits_used_ += vlc_num_bits_block(bb[m][b]);
 
1157
                        _dv_quant(bb[m][b], smallest_qno[m], bl->class_no);
 
1158
                        bits_used_ += _dv_vlc_num_bits_block(bb[m][b]);
1150
1159
                }
1151
1160
#if 0
1152
1161
                fprintf(stderr, "(qno: %d, gain: %d, run: %d) ", 
1164
1173
                if (smallest_qno[m] != 15) { 
1165
1174
                        for (b = 0; b < 6; b++) {
1166
1175
                                dv_block_t *bl = &mb->b[b];
1167
 
                                quant(bl->coeffs,smallest_qno[m],bl->class_no);
 
1176
                                _dv_quant(bl->coeffs,smallest_qno[m],bl->class_no);
1168
1177
                                vlc_encode_block(bl->coeffs,vblocks+6 * m + b);
1169
1178
                        }
1170
1179
                } else {
1310
1319
        } 
1311
1320
}
1312
1321
 
1313
 
int encoder_loop(dv_enc_input_filter_t * input,
 
1322
int dv_encoder_loop(dv_enc_input_filter_t * input,
1314
1323
                 dv_enc_audio_input_filter_t * audio_input,
1315
1324
                 dv_enc_output_filter_t * output,
1316
1325
                 int start, int end, const char* filename,
1397
1406
                        skip_frame_count -= 65536;
1398
1407
                        skipped = 1;
1399
1408
                }
1400
 
                if (output->store(target, audio_info, FALSE, 
 
1409
                if (output->store(target, audio_info, FALSE,
1401
1410
                                  isPAL, is16x9,now) < 0) {
1402
1411
                        return -1;
1403
1412
                }
1412
1421
        return 0;
1413
1422
}
1414
1423
 
1415
 
void show_statistics()
 
1424
void dv_show_statistics()
1416
1425
{
1417
1426
        int i = 0;
1418
1427
        fprintf(stderr, "\n\nFinal statistics:\n"
1452
1461
 *  This function allocates memory for you that must be set free using
1453
1462
 *  dv_encoder_free()
1454
1463
 * 
1455
 
 * @param rem_ntsc_setup A boolean that indicates if 16 should be
1456
 
 *          subtracted from the luma channel to account for the NTSC
1457
 
 *          luma setup/platform of 7.5 IRE. The North American NTSC 
1458
 
 *          standard calls for a 7.5 IRE setup, but DV is based on the
1459
 
 *          Japanese NTSC standard that uses 0 IRE setup. Therefore,
1460
 
 *          North American DV equipments adds the 7.5 IRE setup to its
1461
 
 *          analog output. Without setting this true, computer generated
1462
 
 *          graphics may appear too bright.
1463
 
 *          Typically, this should be set false for PAL and Japanese NTSC
1464
 
 *          users.
 
1464
 * @param ignored A retired option.
1465
1465
 * @param clamp_luma A boolean to indicate that luma values should be 
1466
1466
 *          trimmed to their ITU-R 601 legal limits. When this is false, it
1467
1467
 *          preserves superblack and superwhite.
1471
1471
 *          Typically this should be set true.
1472
1472
 */
1473
1473
dv_encoder_t * 
1474
 
dv_encoder_new(int rem_ntsc_setup, int clamp_luma, int clamp_chroma) {
 
1474
dv_encoder_new(int ignored, int clamp_luma, int clamp_chroma) {
1475
1475
  dv_encoder_t *result;
1476
1476
  
1477
1477
  result = (dv_encoder_t *)calloc(1,sizeof(dv_encoder_t));
1478
1478
  if(!result) return(NULL);
1479
1479
  
 
1480
  dv_init( clamp_luma, clamp_chroma);
 
1481
 
1480
1482
  result->img_y = (short*) calloc(DV_PAL_HEIGHT * DV_WIDTH, sizeof(short));
1481
1483
  if(!result->img_y) goto no_y;
1482
1484
  result->img_cr = (short*) calloc(DV_PAL_HEIGHT * DV_WIDTH / 2, sizeof(short));
1484
1486
  result->img_cb = (short*) calloc(DV_PAL_HEIGHT * DV_WIDTH / 2, sizeof(short));
1485
1487
  if(!result->img_cb) goto no_cb;
1486
1488
 
1487
 
  result->rem_ntsc_setup = rem_ntsc_setup;
 
1489
  result->rem_ntsc_setup = FALSE;
1488
1490
  result->clamp_luma = clamp_luma;
1489
1491
  result->clamp_chroma = clamp_chroma;
1490
 
 
1491
 
  dv_init( clamp_luma, clamp_chroma);
1492
 
 
 
1492
  result->force_dct = DV_DCT_AUTO;
 
1493
 
 
1494
  result->frame_count = 0;
1493
1495
  return(result);
1494
1496
  
1495
1497
no_cb:
1523
1525
} /* dv_encoder_free */
1524
1526
 
1525
1527
 
 
1528
/** @brief Free the dynamically allocated global memory.
 
1529
 *
 
1530
 * This function deallocates the memory allocated by dv_init().
 
1531
 *
 
1532
 */
 
1533
void dv_cleanup(void) {
 
1534
  /* the encoder allocates mem on the heap and must be free-ed */
 
1535
  if (vlc_encode_lookup != NULL) {
 
1536
    free(vlc_encode_lookup);
 
1537
    vlc_encode_lookup = NULL;
 
1538
  }
 
1539
  if (vlc_num_bits_lookup != NULL) {
 
1540
    free(vlc_num_bits_lookup);
 
1541
    vlc_num_bits_lookup = NULL;
 
1542
  }
 
1543
} /* dv_cleanup */
 
1544
 
 
1545
 
1526
1546
int dv_encode_videosegment( dv_encoder_t *dv_enc,
1527
1547
                                dv_videosegment_t *videoseg, uint8_t *vsbuffer)
1528
1548
{
1544
1564
                } else {
1545
1565
                        dv_place_411_macroblock(mb);
1546
1566
                }
1547
 
                ycb_fill_macroblock(dv_enc, mb);
 
1567
                _dv_ycb_fill_macroblock(dv_enc, mb);
1548
1568
                do_dct(mb);
1549
1569
                do_classify(mb, dv_enc->static_qno);
1550
1570
        }
1575
1595
                        "specified: %d!\n", dv_enc->vlc_encode_passes);
1576
1596
                exit(-1);
1577
1597
        }
1578
 
                
 
1598
 
1579
1599
        for (m = 0, mb = videoseg->mb; m < 5; m++, mb++) {
1580
1600
                put_bits(vsbuffer, (8 * (80 * m)) + 28, 4, mb->qno);
1581
1601
                
1600
1620
        return 0;
1601
1621
}
1602
1622
 
1603
 
 
1604
 
static void yuy2_to_ycb( uint8_t *data, int isPAL, short *img_y, short *img_cr, short *img_cb)
 
1623
/* ---------------------------------------------------------------------------
 
1624
 */
 
1625
static void
 
1626
yuy2_to_ycb( uint8_t *data, int isPAL, short *img_y, short *img_cr, short *img_cb)
1605
1627
{
1606
1628
        register int total = (DV_WIDTH * (isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT)) >> 1;
1607
1629
        register int i;
1608
1630
        register uint8_t *p = data;
1609
 
        
 
1631
 
1610
1632
        for (i = 0; i < total; i++) {
1611
1633
                img_y[i*2]   = (((short) *p++) - 128) << DCT_YUV_PRECISION;
1612
1634
                img_cb[i]    = (((short) *p++) - 128) << DCT_YUV_PRECISION;
1617
1639
 
1618
1640
 
1619
1641
#ifdef YUV_420_USE_YV12
1620
 
static void yv12_to_ycb( uint8_t **in, int isPAL, short *img_y, short *img_cr, short *img_cb)
 
1642
/* ---------------------------------------------------------------------------
 
1643
 */
 
1644
static void
 
1645
yv12_to_ycb( uint8_t **in, int isPAL, short *img_y, short *img_cr, short *img_cb)
1621
1646
{
1622
 
        register int total = (DV_WIDTH * (isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT)) >> 1;
1623
 
        register int i;
1624
 
        
1625
 
        for (i = 0; i < total; i++) {
1626
 
                img_y[i*2]   = (((short) in[0][i*2]) - 128) << DCT_YUV_PRECISION;
1627
 
                img_cb[i]    = (((short) in[1][i]) - 128) << DCT_YUV_PRECISION;
1628
 
                img_y[i*2+1] = (((short) in[0][i*2+1]) - 128) << DCT_YUV_PRECISION;
1629
 
                img_cr[i]    = (((short) in[2][i]) - 128) << DCT_YUV_PRECISION;
1630
 
        }
 
1647
        register int total = DV_WIDTH * (isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT);
 
1648
        register int i, j, k;
 
1649
        
 
1650
        
 
1651
        for (i = 0; i < total; i++) img_y[i]   = (((short) in[0][i]) - 128) << DCT_YUV_PRECISION;
 
1652
        
 
1653
        for (i = 0; i < (isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT)/2; ++i) {
 
1654
          for (j = 0; j < DV_WIDTH/2 ; j++) {
 
1655
            
 
1656
            k = i * DV_WIDTH/2 + j;
 
1657
            
 
1658
            img_cb[2*i*DV_WIDTH/2+j]    = (((short) in[1][k]) - 128) << DCT_YUV_PRECISION;
 
1659
            img_cr[2*i*DV_WIDTH/2+j]    = (((short) in[2][k]) - 128) << DCT_YUV_PRECISION;
 
1660
            img_cb[(2*i+1)*DV_WIDTH/2+j]    = (((short) in[1][k]) - 128) << DCT_YUV_PRECISION;
 
1661
            img_cr[(2*i+1)*DV_WIDTH/2+j]    = (((short) in[2][k]) - 128) << DCT_YUV_PRECISION;
 
1662
          }//col-j
 
1663
        }//row-i
 
1664
 
1631
1665
}
1632
1666
#endif
1633
1667
 
1661
1695
 * @param out A pointer to the output buffer, which should alreayd be
1662
1696
 *          allocated width*height*2 bytes. The function will clear the buffer
1663
1697
 *          before filling it.
 
1698
 * @return -1 for failure, 0 for success
1664
1699
 */
1665
1700
int dv_encode_full_frame(dv_encoder_t *dv_enc, uint8_t **in,
1666
1701
                        dv_color_space_t color_space, uint8_t *out)
1672
1707
        unsigned int offset = 0;
1673
1708
        uint8_t *target = out;
1674
1709
        time_t now;
 
1710
        static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
1711
        
1675
1712
        now = time(NULL);
1676
 
        
1677
1713
        if (dv_enc->vlc_encode_passes < 1 || dv_enc->vlc_encode_passes > 3)
1678
1714
                dv_enc->vlc_encode_passes = 3;
1679
1715
        if (dv_enc->static_qno < 1 || dv_enc->static_qno > 2)
1683
1719
 
1684
1720
        memset(out, 0, 480 * (dv_enc->isPAL ? 300 : 250));
1685
1721
 
 
1722
        pthread_mutex_lock(&mutex);
1686
1723
        switch (color_space) {
1687
1724
        case e_dv_color_rgb:
1688
1725
                dv_enc_rgb_to_ycb(in[0], (dv_enc->isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT),
1698
1735
        default:
1699
1736
                fprintf(stderr, "Invalid value for color_space "
1700
1737
                        "specified: %d!\n", (int) color_space);
 
1738
                pthread_mutex_unlock(&mutex);
1701
1739
                return -1;
1702
1740
        }
1703
1741
        
1704
 
        if (dv_enc->rem_ntsc_setup == TRUE) {
 
1742
        if (dv_enc->isPAL == FALSE && dv_enc->rem_ntsc_setup == TRUE) {
1705
1743
                for (i = 0;
1706
1744
                        i < (DV_WIDTH * (dv_enc->isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT));
1707
1745
                        dv_enc->img_y[i++] -= (32) );
1746
1784
                   the VLC data */
1747
1785
                /* Loop through video segments */
1748
1786
                for (v = 0; v < 27; v++) {
1749
 
                        /* skip audio block - 
 
1787
                        /* skip audio block -
1750
1788
                           interleaved before every 3rd video segment
1751
1789
                        */
1752
1790
 
1760
1798
 
1761
1799
                        if (dv_encode_videosegment(dv_enc, &videoseg, target + offset) < 0) {
1762
1800
                                fprintf(stderr, "Enocder failed to process video segment.");
 
1801
                                pthread_mutex_unlock(&mutex);
1763
1802
                                return -1;
1764
1803
                        }
1765
1804
                        
1767
1806
                } 
1768
1807
        } 
1769
1808
        
1770
 
        write_meta_data(target, 0, dv_enc->isPAL, dv_enc->is16x9, &now);
 
1809
        _dv_write_meta_data(target, dv_enc->frame_count++, dv_enc->isPAL, dv_enc->is16x9, &now);
 
1810
 
 
1811
        pthread_mutex_unlock(&mutex);
1771
1812
 
1772
1813
        return 0;
1773
1814
}
1774
1815
 
1775
 
void swab(void*, void*, ssize_t);
1776
 
 
 
1816
#ifdef __linux__
 
1817
void swab(const void*, void*, ssize_t);
 
1818
#endif
1777
1819
 
1778
1820
/** @brief Encode signed 16-bit integer PCM audio data into a frame of DV video.
1779
1821
 *
1786
1828
 * @param frequency The sampling rate of the input must be one of 32000,
1787
1829
 *          44100, or 48000.
1788
1830
 * @param frame_buf A pointer to a DV frame
 
1831
 * @return -1 for failure, 0 for success
1789
1832
 * 
1790
1833
 * @todo handle 4 channels
1791
1834
 */
1800
1843
        audio.bitspersample = 16;
1801
1844
        audio.bytealignment = 4;
1802
1845
        audio.bytespersecond = frequency * audio.bytealignment;
1803
 
        audio.bytesperframe = audio.bytespersecond/(dv_enc->isPAL ? 25 : 30); /* not used */
 
1846
 
 
1847
        /* estimate the number of samples per frame if not specified. */
 
1848
        dv_enc->isPAL = frame_buf[ 3 ] & 0x80;
 
1849
        if ( dv_enc->samples_this_frame == 0 )
 
1850
                audio.bytesperframe = audio.bytespersecond/(dv_enc->isPAL ? 25 : 30);
 
1851
        else
 
1852
                audio.bytesperframe = dv_enc->samples_this_frame;
 
1853
 
1804
1854
 
1805
1855
        /* interleave channels */
1806
1856
        if (channels > 1) {
1809
1859
                                swab( pcm[j]+i, audio.data + (i*2+j)*channels, 2);
1810
1860
        }
1811
1861
 
1812
 
        return raw_insert_audio(frame_buf, &audio, dv_enc->isPAL);
 
1862
        return _dv_raw_insert_audio(frame_buf, &audio, dv_enc->isPAL);
 
1863
}
 
1864
 
 
1865
/** @brief Calculate number of samples to be applied to a new frame.
 
1866
 *
 
1867
 * @param dv_enc A pointer to a dv_encoder_t struct containing relevant options:
 
1868
 *        -isPAL  Set true (non-zero) to encode the data in PAL format.
 
1869
 * @param frequency The sampling rate of the input must be one of 32000,
 
1870
 *          44100, or 48000.
 
1871
 * @param frame_count a sequential number representing the frame number.
 
1872
 * @return number of samples associated to the frame or 0 if invalid frequency
 
1873
 *                      received.
 
1874
 *
 
1875
 * @todo confirm samples returned for all frequencies
 
1876
 */
 
1877
 
 
1878
int dv_calculate_samples( dv_encoder_t *encoder, int frequency, int frame_count )
 
1879
{
 
1880
        int samples = 0;
 
1881
 
 
1882
        if ( encoder->isPAL )
 
1883
        {
 
1884
                samples = frequency / 25;
 
1885
 
 
1886
                switch( frequency )
 
1887
                {
 
1888
                        case 48000:
 
1889
                                if ( frame_count % 25 == 0 )
 
1890
                                        samples --;
 
1891
                                break;
 
1892
                        case 44100:
 
1893
                        case 32000:
 
1894
                                break;
 
1895
                        default:
 
1896
                                samples = 0;
 
1897
                                break;
 
1898
                }
 
1899
        }
 
1900
        else
 
1901
        {
 
1902
                samples = frequency / 30;
 
1903
 
 
1904
                switch ( frequency )
 
1905
                {
 
1906
                        case 48000:
 
1907
                                if ( frame_count % 5 != 0 )
 
1908
                                        samples += 2;
 
1909
                                break;
 
1910
                        case 44100:
 
1911
                                if ( frame_count % 300 == 0 )
 
1912
                                        samples = 1471;
 
1913
                                else if ( frame_count % 30 == 0 )
 
1914
                                        samples = 1470;
 
1915
                                else if ( frame_count % 2 == 0 )
 
1916
                                        samples = 1472;
 
1917
                                else
 
1918
                                        samples = 1471;
 
1919
                                break;
 
1920
                        case 32000:
 
1921
                                if ( frame_count % 30 == 0 )
 
1922
                                        samples = 1068;
 
1923
                                else if ( frame_count % 29 == 0 )
 
1924
                                        samples = 1067;
 
1925
                                else if ( frame_count % 4 == 2 )
 
1926
                                        samples = 1067;
 
1927
                                else
 
1928
                                        samples = 1068;
 
1929
                                break;
 
1930
                        default:
 
1931
                                samples = 0;
 
1932
                }
 
1933
        }
 
1934
 
 
1935
        encoder->samples_this_frame = samples;
 
1936
 
 
1937
        return samples;
1813
1938
}