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.
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.
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.
24
24
* The libdv homepage is http://libdv.sourceforge.net/.
319
324
static dv_vlc_encode_t * vlc_test_lookup[512];
321
void init_vlc_test_lookup()
326
void _dv_init_vlc_test_lookup()
324
329
memset(vlc_test_lookup, 0, 512 * sizeof(dv_vlc_encode_t*));
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;
413
void init_vlc_encode_lookup(void)
418
void _dv_init_vlc_encode_lookup(void)
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);
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);
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
468
void prepare_reorder_tables(void)
477
void _dv_prepare_reorder_tables(void)
471
480
for (i = 0; i < 64; i++) {
474
483
reorder_248[i]--;
475
484
reorder_248[i] *= 2;
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);
482
491
static void reorder_block(dv_block_t *bl)
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);
509
518
static unsigned long vlc_encode_block(dv_coeff_t* coeffs, dv_vlc_block_t* out)
541
num_bits = vlc_encode_block_mmx(coeffs, &o);
550
num_bits = _dv_vlc_encode_block_mmx(coeffs, &o);
544
553
*o++ = set_dv_vlc(0x6, 4); /* EOB */
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);
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)
557
566
dv_coeff_t * z = coeffs + 1; /* First AC coeff */
619
628
*e = set_dv_vlc(get_dv_vlc_val(*e) & ((1 << len)-1), len);
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;
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,
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);
730
inline int classify(dv_coeff_t * bl)
739
static inline int classify(dv_coeff_t * bl)
733
742
static unsigned short amp_ofs[3][4] = {
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])) {
792
801
dv_block_t *bl = &mb->b[b];
794
803
if (bl->dct_mode == DV_DCT_88) {
804
_dv_dct_88(bl->coeffs);
797
806
reorder_block(bl);
799
808
#if BRUTE_FORCE_DCT_88
800
weight_88(bl->coeffs);
809
_dv_weight_88(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);
809
818
dct_used[bl->dct_mode]++;
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)
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);
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;
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]);
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);
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);
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]);
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);
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;
1400
if (output->store(target, audio_info, FALSE,
1409
if (output->store(target, audio_info, FALSE,
1401
1410
isPAL, is16x9,now) < 0) {
1452
1461
* This function allocates memory for you that must be set free using
1453
1462
* dv_encoder_free()
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
* @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.
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;
1477
1477
result = (dv_encoder_t *)calloc(1,sizeof(dv_encoder_t));
1478
1478
if(!result) return(NULL);
1480
dv_init( clamp_luma, clamp_chroma);
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;
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;
1491
dv_init( clamp_luma, clamp_chroma);
1492
result->force_dct = DV_DCT_AUTO;
1494
result->frame_count = 0;
1493
1495
return(result);
1523
1525
} /* dv_encoder_free */
1528
/** @brief Free the dynamically allocated global memory.
1530
* This function deallocates the memory allocated by dv_init().
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;
1539
if (vlc_num_bits_lookup != NULL) {
1540
free(vlc_num_bits_lookup);
1541
vlc_num_bits_lookup = NULL;
1526
1546
int dv_encode_videosegment( dv_encoder_t *dv_enc,
1527
1547
dv_videosegment_t *videoseg, uint8_t *vsbuffer)
1545
1565
dv_place_411_macroblock(mb);
1547
ycb_fill_macroblock(dv_enc, mb);
1567
_dv_ycb_fill_macroblock(dv_enc, mb);
1549
1569
do_classify(mb, dv_enc->static_qno);
1575
1595
"specified: %d!\n", dv_enc->vlc_encode_passes);
1579
1599
for (m = 0, mb = videoseg->mb; m < 5; m++, mb++) {
1580
1600
put_bits(vsbuffer, (8 * (80 * m)) + 28, 4, mb->qno);
1604
static void yuy2_to_ycb( uint8_t *data, int isPAL, short *img_y, short *img_cr, short *img_cb)
1623
/* ---------------------------------------------------------------------------
1626
yuy2_to_ycb( uint8_t *data, int isPAL, short *img_y, short *img_cr, short *img_cb)
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;
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;
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
/* ---------------------------------------------------------------------------
1645
yv12_to_ycb( uint8_t **in, int isPAL, short *img_y, short *img_cr, short *img_cb)
1622
register int total = (DV_WIDTH * (isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT)) >> 1;
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;
1647
register int total = DV_WIDTH * (isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT);
1648
register int i, j, k;
1651
for (i = 0; i < total; i++) img_y[i] = (((short) in[0][i]) - 128) << DCT_YUV_PRECISION;
1653
for (i = 0; i < (isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT)/2; ++i) {
1654
for (j = 0; j < DV_WIDTH/2 ; j++) {
1656
k = i * DV_WIDTH/2 + j;
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;
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
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;
1710
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1675
1712
now = time(NULL);
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)
1684
1720
memset(out, 0, 480 * (dv_enc->isPAL ? 300 : 250));
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),
1699
1736
fprintf(stderr, "Invalid value for color_space "
1700
1737
"specified: %d!\n", (int) color_space);
1738
pthread_mutex_unlock(&mutex);
1704
if (dv_enc->rem_ntsc_setup == TRUE) {
1742
if (dv_enc->isPAL == FALSE && dv_enc->rem_ntsc_setup == TRUE) {
1706
1744
i < (DV_WIDTH * (dv_enc->isPAL ? DV_PAL_HEIGHT : DV_NTSC_HEIGHT));
1707
1745
dv_enc->img_y[i++] -= (32) );
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);
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);
1811
pthread_mutex_unlock(&mutex);
1775
void swab(void*, void*, ssize_t);
1817
void swab(const void*, void*, ssize_t);
1778
1820
/** @brief Encode signed 16-bit integer PCM audio data into a frame of DV video.
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
1790
1833
* @todo handle 4 channels
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 */
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);
1852
audio.bytesperframe = dv_enc->samples_this_frame;
1805
1855
/* interleave channels */
1806
1856
if (channels > 1) {
1809
1859
swab( pcm[j]+i, audio.data + (i*2+j)*channels, 2);
1812
return raw_insert_audio(frame_buf, &audio, dv_enc->isPAL);
1862
return _dv_raw_insert_audio(frame_buf, &audio, dv_enc->isPAL);
1865
/** @brief Calculate number of samples to be applied to a new frame.
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,
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
1875
* @todo confirm samples returned for all frequencies
1878
int dv_calculate_samples( dv_encoder_t *encoder, int frequency, int frame_count )
1882
if ( encoder->isPAL )
1884
samples = frequency / 25;
1889
if ( frame_count % 25 == 0 )
1902
samples = frequency / 30;
1904
switch ( frequency )
1907
if ( frame_count % 5 != 0 )
1911
if ( frame_count % 300 == 0 )
1913
else if ( frame_count % 30 == 0 )
1915
else if ( frame_count % 2 == 0 )
1921
if ( frame_count % 30 == 0 )
1923
else if ( frame_count % 29 == 0 )
1925
else if ( frame_count % 4 == 2 )
1935
encoder->samples_this_frame = samples;