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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
|
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* TODO: - We currently don't handle discontinuities in the stream in a useful
* way and instead rely on the developer plugging in audiorate if
* the stream contains discontinuities.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <gstflacenc.h>
#include <gst/audio/audio.h>
#include <gst/audio/multichannel.h>
#include <gst/tag/tag.h>
#include <gst/gsttagsetter.h>
/* Taken from http://flac.sourceforge.net/format.html#frame_header */
static const GstAudioChannelPosition channel_positions[8][8] = {
{GST_AUDIO_CHANNEL_POSITION_FRONT_MONO},
{GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_LFE,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
/* FIXME: 7/8 channel layouts are not defined in the FLAC specs */
{
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_LFE,
GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_LFE,
GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}
};
#define FLAC_SINK_CAPS \
"audio/x-raw-int, " \
"endianness = (int) BYTE_ORDER, " \
"signed = (boolean) TRUE, " \
"width = (int) 8, " \
"depth = (int) 8, " \
"rate = (int) [ 1, 655350 ], " \
"channels = (int) [ 1, 8 ]; " \
"audio/x-raw-int, " \
"endianness = (int) BYTE_ORDER, " \
"signed = (boolean) TRUE, " \
"width = (int) 16, " \
"depth = (int) { 12, 16 }, " \
"rate = (int) [ 1, 655350 ], " \
"channels = (int) [ 1, 8 ]; " \
"audio/x-raw-int, " \
"endianness = (int) BYTE_ORDER, " \
"signed = (boolean) TRUE, " \
"width = (int) 32, " \
"depth = (int) { 20, 24 }, " \
"rate = (int) [ 1, 655350 ], " \
"channels = (int) [ 1, 8 ]"
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-flac")
);
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (FLAC_SINK_CAPS)
);
enum
{
PROP_0,
PROP_QUALITY,
PROP_STREAMABLE_SUBSET,
PROP_MID_SIDE_STEREO,
PROP_LOOSE_MID_SIDE_STEREO,
PROP_BLOCKSIZE,
PROP_MAX_LPC_ORDER,
PROP_QLP_COEFF_PRECISION,
PROP_QLP_COEFF_PREC_SEARCH,
PROP_ESCAPE_CODING,
PROP_EXHAUSTIVE_MODEL_SEARCH,
PROP_MIN_RESIDUAL_PARTITION_ORDER,
PROP_MAX_RESIDUAL_PARTITION_ORDER,
PROP_RICE_PARAMETER_SEARCH_DIST
};
GST_DEBUG_CATEGORY_STATIC (flacenc_debug);
#define GST_CAT_DEFAULT flacenc_debug
#define _do_init(type) \
G_STMT_START{ \
static const GInterfaceInfo tag_setter_info = { \
NULL, \
NULL, \
NULL \
}; \
g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, \
&tag_setter_info); \
}G_STMT_END
GST_BOILERPLATE_FULL (GstFlacEnc, gst_flac_enc, GstElement, GST_TYPE_ELEMENT,
_do_init);
static void gst_flac_enc_finalize (GObject * object);
static gboolean gst_flac_enc_sink_setcaps (GstPad * pad, GstCaps * caps);
static GstCaps *gst_flac_enc_sink_getcaps (GstPad * pad);
static gboolean gst_flac_enc_sink_event (GstPad * pad, GstEvent * event);
static GstFlowReturn gst_flac_enc_chain (GstPad * pad, GstBuffer * buffer);
static gboolean gst_flac_enc_update_quality (GstFlacEnc * flacenc,
gint quality);
static void gst_flac_enc_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_flac_enc_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static GstStateChangeReturn gst_flac_enc_change_state (GstElement * element,
GstStateChange transition);
#ifdef LEGACY_FLAC
static FLAC__StreamEncoderWriteStatus
gst_flac_enc_write_callback (const FLAC__SeekableStreamEncoder * encoder,
const FLAC__byte buffer[], unsigned bytes,
unsigned samples, unsigned current_frame, void *client_data);
static FLAC__SeekableStreamEncoderSeekStatus
gst_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder * encoder,
FLAC__uint64 absolute_byte_offset, void *client_data);
static FLAC__SeekableStreamEncoderTellStatus
gst_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder * encoder,
FLAC__uint64 * absolute_byte_offset, void *client_data);
#else
static FLAC__StreamEncoderWriteStatus
gst_flac_enc_write_callback (const FLAC__StreamEncoder * encoder,
const FLAC__byte buffer[], size_t bytes,
unsigned samples, unsigned current_frame, void *client_data);
static FLAC__StreamEncoderSeekStatus
gst_flac_enc_seek_callback (const FLAC__StreamEncoder * encoder,
FLAC__uint64 absolute_byte_offset, void *client_data);
static FLAC__StreamEncoderTellStatus
gst_flac_enc_tell_callback (const FLAC__StreamEncoder * encoder,
FLAC__uint64 * absolute_byte_offset, void *client_data);
#endif
typedef struct
{
gboolean exhaustive_model_search;
gboolean escape_coding;
gboolean mid_side;
gboolean loose_mid_side;
guint qlp_coeff_precision;
gboolean qlp_coeff_prec_search;
guint min_residual_partition_order;
guint max_residual_partition_order;
guint rice_parameter_search_dist;
guint max_lpc_order;
guint blocksize;
}
GstFlacEncParams;
static const GstFlacEncParams flacenc_params[] = {
{FALSE, FALSE, FALSE, FALSE, 0, FALSE, 2, 2, 0, 0, 1152},
{FALSE, FALSE, TRUE, TRUE, 0, FALSE, 2, 2, 0, 0, 1152},
{FALSE, FALSE, TRUE, FALSE, 0, FALSE, 0, 3, 0, 0, 1152},
{FALSE, FALSE, FALSE, FALSE, 0, FALSE, 3, 3, 0, 6, 4608},
{FALSE, FALSE, TRUE, TRUE, 0, FALSE, 3, 3, 0, 8, 4608},
{FALSE, FALSE, TRUE, FALSE, 0, FALSE, 3, 3, 0, 8, 4608},
{FALSE, FALSE, TRUE, FALSE, 0, FALSE, 0, 4, 0, 8, 4608},
{TRUE, FALSE, TRUE, FALSE, 0, FALSE, 0, 6, 0, 8, 4608},
{TRUE, FALSE, TRUE, FALSE, 0, FALSE, 0, 6, 0, 12, 4608},
{TRUE, TRUE, TRUE, FALSE, 0, FALSE, 0, 16, 0, 32, 4608},
};
#define DEFAULT_QUALITY 5
#define GST_TYPE_FLAC_ENC_QUALITY (gst_flac_enc_quality_get_type ())
GType
gst_flac_enc_quality_get_type (void)
{
static GType qtype = 0;
if (qtype == 0) {
static const GEnumValue values[] = {
{0, "0 - Fastest compression", "0"},
{1, "1", "1"},
{2, "2", "2"},
{3, "3", "3"},
{4, "4", "4"},
{5, "5 - Default", "5"},
{6, "6", "6"},
{7, "7", "7"},
{8, "8 - Highest compression", "8"},
{9, "9 - Insane", "9"},
{0, NULL, NULL}
};
qtype = g_enum_register_static ("GstFlacEncQuality", values);
}
return qtype;
}
static void
gst_flac_enc_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&src_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_factory));
gst_element_class_set_details_simple (element_class, "FLAC audio encoder",
"Codec/Encoder/Audio",
"Encodes audio with the FLAC lossless audio encoder",
"Wim Taymans <wim.taymans@chello.be>");
GST_DEBUG_CATEGORY_INIT (flacenc_debug, "flacenc", 0,
"Flac encoding element");
}
static void
gst_flac_enc_class_init (GstFlacEncClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gobject_class->set_property = gst_flac_enc_set_property;
gobject_class->get_property = gst_flac_enc_get_property;
gobject_class->finalize = gst_flac_enc_finalize;
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
g_param_spec_enum ("quality",
"Quality",
"Speed versus compression tradeoff",
GST_TYPE_FLAC_ENC_QUALITY, DEFAULT_QUALITY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_STREAMABLE_SUBSET, g_param_spec_boolean ("streamable_subset",
"Streamable subset",
"true to limit encoder to generating a Subset stream, else false",
TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MID_SIDE_STEREO,
g_param_spec_boolean ("mid_side_stereo", "Do mid side stereo",
"Do mid side stereo (only for stereo input)",
flacenc_params[DEFAULT_QUALITY].mid_side,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_LOOSE_MID_SIDE_STEREO, g_param_spec_boolean ("loose_mid_side_stereo",
"Loose mid side stereo", "Loose mid side stereo",
flacenc_params[DEFAULT_QUALITY].loose_mid_side,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
g_param_spec_uint ("blocksize", "Blocksize", "Blocksize in samples",
FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE,
flacenc_params[DEFAULT_QUALITY].blocksize,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_LPC_ORDER,
g_param_spec_uint ("max_lpc_order", "Max LPC order",
"Max LPC order; 0 => use only fixed predictors", 0,
FLAC__MAX_LPC_ORDER, flacenc_params[DEFAULT_QUALITY].max_lpc_order,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_QLP_COEFF_PRECISION, g_param_spec_uint ("qlp_coeff_precision",
"QLP coefficients precision",
"Precision in bits of quantized linear-predictor coefficients; 0 = automatic",
0, 32, flacenc_params[DEFAULT_QUALITY].qlp_coeff_precision,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_QLP_COEFF_PREC_SEARCH, g_param_spec_boolean ("qlp_coeff_prec_search",
"Do QLP coefficients precision search",
"false = use qlp_coeff_precision, "
"true = search around qlp_coeff_precision, take best",
flacenc_params[DEFAULT_QUALITY].qlp_coeff_prec_search,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ESCAPE_CODING,
g_param_spec_boolean ("escape_coding", "Do Escape coding",
"search for escape codes in the entropy coding stage "
"for slightly better compression",
flacenc_params[DEFAULT_QUALITY].escape_coding,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_EXHAUSTIVE_MODEL_SEARCH,
g_param_spec_boolean ("exhaustive_model_search",
"Do exhaustive model search",
"do exhaustive search of LP coefficient quantization (expensive!)",
flacenc_params[DEFAULT_QUALITY].exhaustive_model_search,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_MIN_RESIDUAL_PARTITION_ORDER,
g_param_spec_uint ("min_residual_partition_order",
"Min residual partition order",
"Min residual partition order (above 4 doesn't usually help much)", 0,
16, flacenc_params[DEFAULT_QUALITY].min_residual_partition_order,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_MAX_RESIDUAL_PARTITION_ORDER,
g_param_spec_uint ("max_residual_partition_order",
"Max residual partition order",
"Max residual partition order (above 4 doesn't usually help much)", 0,
16, flacenc_params[DEFAULT_QUALITY].max_residual_partition_order,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_RICE_PARAMETER_SEARCH_DIST,
g_param_spec_uint ("rice_parameter_search_dist",
"rice_parameter_search_dist",
"0 = try only calc'd parameter k; else try all [k-dist..k+dist] "
"parameters, use best", 0, FLAC__MAX_RICE_PARTITION_ORDER,
flacenc_params[DEFAULT_QUALITY].rice_parameter_search_dist,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
gstelement_class->change_state = gst_flac_enc_change_state;
}
static void
gst_flac_enc_init (GstFlacEnc * flacenc, GstFlacEncClass * klass)
{
flacenc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
gst_pad_set_chain_function (flacenc->sinkpad,
GST_DEBUG_FUNCPTR (gst_flac_enc_chain));
gst_pad_set_event_function (flacenc->sinkpad,
GST_DEBUG_FUNCPTR (gst_flac_enc_sink_event));
gst_pad_set_getcaps_function (flacenc->sinkpad,
GST_DEBUG_FUNCPTR (gst_flac_enc_sink_getcaps));
gst_pad_set_setcaps_function (flacenc->sinkpad,
GST_DEBUG_FUNCPTR (gst_flac_enc_sink_setcaps));
gst_element_add_pad (GST_ELEMENT (flacenc), flacenc->sinkpad);
flacenc->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
gst_pad_use_fixed_caps (flacenc->srcpad);
gst_element_add_pad (GST_ELEMENT (flacenc), flacenc->srcpad);
#ifdef LEGACY_FLAC
flacenc->encoder = FLAC__seekable_stream_encoder_new ();
#else
flacenc->encoder = FLAC__stream_encoder_new ();
#endif
flacenc->offset = 0;
flacenc->samples_written = 0;
flacenc->channels = 0;
gst_flac_enc_update_quality (flacenc, DEFAULT_QUALITY);
flacenc->tags = gst_tag_list_new ();
flacenc->got_headers = FALSE;
flacenc->headers = NULL;
flacenc->last_flow = GST_FLOW_OK;
}
static void
gst_flac_enc_finalize (GObject * object)
{
GstFlacEnc *flacenc = GST_FLAC_ENC (object);
gst_tag_list_free (flacenc->tags);
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_delete (flacenc->encoder);
#else
FLAC__stream_encoder_delete (flacenc->encoder);
#endif
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
{
GList *comments;
GList *it;
GstFlacEnc *flacenc = GST_FLAC_ENC (user_data);
comments = gst_tag_to_vorbis_comments (list, tag);
for (it = comments; it != NULL; it = it->next) {
FLAC__StreamMetadata_VorbisComment_Entry commment_entry;
commment_entry.length = strlen (it->data);
commment_entry.entry = it->data;
FLAC__metadata_object_vorbiscomment_insert_comment (flacenc->meta[0],
flacenc->meta[0]->data.vorbis_comment.num_comments,
commment_entry, TRUE);
g_free (it->data);
}
g_list_free (comments);
}
static void
gst_flac_enc_set_metadata (GstFlacEnc * flacenc)
{
const GstTagList *user_tags;
GstTagList *copy;
g_return_if_fail (flacenc != NULL);
user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (flacenc));
if ((flacenc->tags == NULL) && (user_tags == NULL)) {
return;
}
copy = gst_tag_list_merge (user_tags, flacenc->tags,
gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (flacenc)));
flacenc->meta = g_malloc (sizeof (FLAC__StreamMetadata **));
flacenc->meta[0] =
FLAC__metadata_object_new (FLAC__METADATA_TYPE_VORBIS_COMMENT);
gst_tag_list_foreach (copy, add_one_tag, flacenc);
#ifdef LEGACY_FLAC
if (FLAC__seekable_stream_encoder_set_metadata (flacenc->encoder,
flacenc->meta, 1) != true)
#else
if (FLAC__stream_encoder_set_metadata (flacenc->encoder,
flacenc->meta, 1) != true)
#endif
g_warning ("Dude, i'm already initialized!");
gst_tag_list_free (copy);
}
static void
gst_flac_enc_caps_append_structure_with_widths (GstCaps * caps,
GstStructure * s)
{
GstStructure *tmp;
GValue list = { 0, };
GValue depth = { 0, };
tmp = gst_structure_copy (s);
gst_structure_set (tmp, "width", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
gst_caps_append_structure (caps, tmp);
tmp = gst_structure_copy (s);
g_value_init (&depth, G_TYPE_INT);
g_value_init (&list, GST_TYPE_LIST);
g_value_set_int (&depth, 12);
gst_value_list_append_value (&list, &depth);
g_value_set_int (&depth, 16);
gst_value_list_append_value (&list, &depth);
gst_structure_set (tmp, "width", G_TYPE_INT, 16, NULL);
gst_structure_set_value (tmp, "depth", &list);
gst_caps_append_structure (caps, tmp);
g_value_reset (&list);
tmp = s;
g_value_set_int (&depth, 20);
gst_value_list_append_value (&list, &depth);
g_value_set_int (&depth, 24);
gst_value_list_append_value (&list, &depth);
gst_structure_set (tmp, "width", G_TYPE_INT, 32, NULL);
gst_structure_set_value (tmp, "depth", &list);
gst_caps_append_structure (caps, tmp);
g_value_unset (&list);
g_value_unset (&depth);
}
static GstCaps *
gst_flac_enc_sink_getcaps (GstPad * pad)
{
GstCaps *ret = NULL;
GST_OBJECT_LOCK (pad);
if (GST_PAD_CAPS (pad)) {
ret = gst_caps_ref (GST_PAD_CAPS (pad));
} else {
gint i, c;
ret = gst_caps_new_empty ();
gst_flac_enc_caps_append_structure_with_widths (ret,
gst_structure_new ("audio/x-raw-int",
"endianness", G_TYPE_INT, G_BYTE_ORDER,
"signed", G_TYPE_BOOLEAN, TRUE,
"rate", GST_TYPE_INT_RANGE, 1, 655350,
"channels", GST_TYPE_INT_RANGE, 1, 2, NULL));
for (i = 3; i <= 8; i++) {
GValue positions = { 0, };
GValue pos = { 0, };
GstStructure *s;
g_value_init (&positions, GST_TYPE_ARRAY);
g_value_init (&pos, GST_TYPE_AUDIO_CHANNEL_POSITION);
for (c = 0; c < i; c++) {
g_value_set_enum (&pos, channel_positions[i - 1][c]);
gst_value_array_append_value (&positions, &pos);
}
g_value_unset (&pos);
s = gst_structure_new ("audio/x-raw-int",
"endianness", G_TYPE_INT, G_BYTE_ORDER,
"signed", G_TYPE_BOOLEAN, TRUE,
"rate", GST_TYPE_INT_RANGE, 1, 655350,
"channels", G_TYPE_INT, i, NULL);
gst_structure_set_value (s, "channel-positions", &positions);
g_value_unset (&positions);
gst_flac_enc_caps_append_structure_with_widths (ret, s);
}
}
GST_OBJECT_UNLOCK (pad);
GST_DEBUG_OBJECT (pad, "Return caps %" GST_PTR_FORMAT, ret);
return ret;
}
static guint64
gst_flac_enc_query_peer_total_samples (GstFlacEnc * flacenc, GstPad * pad)
{
GstFormat fmt = GST_FORMAT_DEFAULT;
gint64 duration;
if (gst_pad_query_peer_duration (pad, &fmt, &duration)
&& fmt == GST_FORMAT_DEFAULT && duration != GST_CLOCK_TIME_NONE)
goto done;
fmt = GST_FORMAT_TIME;
if (gst_pad_query_peer_duration (pad, &fmt, &duration) &&
fmt == GST_FORMAT_TIME && duration != GST_CLOCK_TIME_NONE) {
duration = GST_FRAMES_TO_CLOCK_TIME (duration, flacenc->sample_rate);
goto done;
}
GST_DEBUG_OBJECT (flacenc, "Upstream reported no total samples");
return GST_CLOCK_TIME_NONE;
done:
GST_DEBUG_OBJECT (flacenc,
"Upstream reported %" G_GUINT64_FORMAT " total samples", duration);
return duration;
}
static gboolean
gst_flac_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
{
GstFlacEnc *flacenc;
GstStructure *structure;
guint64 total_samples = GST_CLOCK_TIME_NONE;
#ifdef LEGACY_FLAC
FLAC__SeekableStreamEncoderState state;
#else
FLAC__StreamEncoderInitStatus init_status;
#endif
gint depth, chans, rate, width;
flacenc = GST_FLAC_ENC (gst_pad_get_parent (pad));
#ifdef LEGACY_FLAC
if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) !=
FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
#else
if (FLAC__stream_encoder_get_state (flacenc->encoder) !=
FLAC__STREAM_ENCODER_UNINITIALIZED)
#endif
goto encoder_already_initialized;
structure = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (structure, "channels", &chans) ||
!gst_structure_get_int (structure, "width", &width) ||
!gst_structure_get_int (structure, "depth", &depth) ||
!gst_structure_get_int (structure, "rate", &rate)) {
GST_DEBUG_OBJECT (flacenc, "incomplete caps: %" GST_PTR_FORMAT, caps);
return FALSE;
}
flacenc->channels = chans;
flacenc->width = width;
flacenc->depth = depth;
flacenc->sample_rate = rate;
caps = gst_caps_new_simple ("audio/x-flac",
"channels", G_TYPE_INT, flacenc->channels,
"rate", G_TYPE_INT, flacenc->sample_rate, NULL);
if (!gst_pad_set_caps (flacenc->srcpad, caps))
goto setting_src_caps_failed;
gst_caps_unref (caps);
total_samples = gst_flac_enc_query_peer_total_samples (flacenc, pad);
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_bits_per_sample (flacenc->encoder,
flacenc->depth);
FLAC__seekable_stream_encoder_set_sample_rate (flacenc->encoder,
flacenc->sample_rate);
FLAC__seekable_stream_encoder_set_channels (flacenc->encoder,
flacenc->channels);
if (total_samples != GST_CLOCK_TIME_NONE)
FLAC__seekable_stream_encoder_set_total_samples_estimate (flacenc->encoder,
total_samples);
FLAC__seekable_stream_encoder_set_write_callback (flacenc->encoder,
gst_flac_enc_write_callback);
FLAC__seekable_stream_encoder_set_seek_callback (flacenc->encoder,
gst_flac_enc_seek_callback);
FLAC__seekable_stream_encoder_set_tell_callback (flacenc->encoder,
gst_flac_enc_tell_callback);
FLAC__seekable_stream_encoder_set_client_data (flacenc->encoder, flacenc);
#else
FLAC__stream_encoder_set_bits_per_sample (flacenc->encoder, flacenc->depth);
FLAC__stream_encoder_set_sample_rate (flacenc->encoder, flacenc->sample_rate);
FLAC__stream_encoder_set_channels (flacenc->encoder, flacenc->channels);
if (total_samples != GST_CLOCK_TIME_NONE)
FLAC__stream_encoder_set_total_samples_estimate (flacenc->encoder,
total_samples);
#endif
gst_flac_enc_set_metadata (flacenc);
#ifdef LEGACY_FLAC
state = FLAC__seekable_stream_encoder_init (flacenc->encoder);
if (state != FLAC__STREAM_ENCODER_OK)
goto failed_to_initialize;
#else
init_status = FLAC__stream_encoder_init_stream (flacenc->encoder,
gst_flac_enc_write_callback, gst_flac_enc_seek_callback,
gst_flac_enc_tell_callback, NULL, flacenc);
if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
goto failed_to_initialize;
#endif
gst_object_unref (flacenc);
return TRUE;
encoder_already_initialized:
{
g_warning ("flac already initialized -- fixme allow this");
gst_object_unref (flacenc);
return FALSE;
}
setting_src_caps_failed:
{
GST_DEBUG_OBJECT (flacenc,
"Couldn't set caps on source pad: %" GST_PTR_FORMAT, caps);
gst_caps_unref (caps);
gst_object_unref (flacenc);
return FALSE;
}
failed_to_initialize:
{
GST_ELEMENT_ERROR (flacenc, LIBRARY, INIT, (NULL),
("could not initialize encoder (wrong parameters?)"));
gst_object_unref (flacenc);
return FALSE;
}
}
static gboolean
gst_flac_enc_update_quality (GstFlacEnc * flacenc, gint quality)
{
flacenc->quality = quality;
#ifdef LEGACY_FLAC
#define DO_UPDATE(name, val, str) \
G_STMT_START { \
if (FLAC__seekable_stream_encoder_get_##name (flacenc->encoder) != \
flacenc_params[quality].val) { \
FLAC__seekable_stream_encoder_set_##name (flacenc->encoder, \
flacenc_params[quality].val); \
g_object_notify (G_OBJECT (flacenc), str); \
} \
} G_STMT_END
#else
#define DO_UPDATE(name, val, str) \
G_STMT_START { \
if (FLAC__stream_encoder_get_##name (flacenc->encoder) != \
flacenc_params[quality].val) { \
FLAC__stream_encoder_set_##name (flacenc->encoder, \
flacenc_params[quality].val); \
g_object_notify (G_OBJECT (flacenc), str); \
} \
} G_STMT_END
#endif
g_object_freeze_notify (G_OBJECT (flacenc));
if (flacenc->channels == 2 || flacenc->channels == 0) {
DO_UPDATE (do_mid_side_stereo, mid_side, "mid_side_stereo");
DO_UPDATE (loose_mid_side_stereo, loose_mid_side, "loose_mid_side");
}
DO_UPDATE (blocksize, blocksize, "blocksize");
DO_UPDATE (max_lpc_order, max_lpc_order, "max_lpc_order");
DO_UPDATE (qlp_coeff_precision, qlp_coeff_precision, "qlp_coeff_precision");
DO_UPDATE (do_qlp_coeff_prec_search, qlp_coeff_prec_search,
"qlp_coeff_prec_search");
DO_UPDATE (do_escape_coding, escape_coding, "escape_coding");
DO_UPDATE (do_exhaustive_model_search, exhaustive_model_search,
"exhaustive_model_search");
DO_UPDATE (min_residual_partition_order, min_residual_partition_order,
"min_residual_partition_order");
DO_UPDATE (max_residual_partition_order, max_residual_partition_order,
"max_residual_partition_order");
DO_UPDATE (rice_parameter_search_dist, rice_parameter_search_dist,
"rice_parameter_search_dist");
#undef DO_UPDATE
g_object_thaw_notify (G_OBJECT (flacenc));
return TRUE;
}
#ifdef LEGACY_FLAC
static FLAC__SeekableStreamEncoderSeekStatus
gst_flac_enc_seek_callback (const FLAC__SeekableStreamEncoder * encoder,
FLAC__uint64 absolute_byte_offset, void *client_data)
#else
static FLAC__StreamEncoderSeekStatus
gst_flac_enc_seek_callback (const FLAC__StreamEncoder * encoder,
FLAC__uint64 absolute_byte_offset, void *client_data)
#endif
{
GstFlacEnc *flacenc;
GstEvent *event;
GstPad *peerpad;
flacenc = GST_FLAC_ENC (client_data);
if (flacenc->stopped)
#ifdef LEGACY_FLAC
return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
#else
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
#endif
event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES,
absolute_byte_offset, GST_BUFFER_OFFSET_NONE, 0);
if ((peerpad = gst_pad_get_peer (flacenc->srcpad))) {
gboolean ret = gst_pad_send_event (peerpad, event);
gst_object_unref (peerpad);
if (ret) {
GST_DEBUG ("Seek to %" G_GUINT64_FORMAT " %s", absolute_byte_offset,
"succeeded");
} else {
GST_DEBUG ("Seek to %" G_GUINT64_FORMAT " %s", absolute_byte_offset,
"failed");
#ifdef LEGACY_FLAC
return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR;
#else
return FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
#endif
}
} else {
GST_DEBUG ("Seek to %" G_GUINT64_FORMAT " failed (no peer pad)",
absolute_byte_offset);
}
flacenc->offset = absolute_byte_offset;
#ifdef LEGACY_FLAC
return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
#else
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
#endif
}
static void
notgst_value_array_append_buffer (GValue * array_val, GstBuffer * buf)
{
GValue value = { 0, };
g_value_init (&value, GST_TYPE_BUFFER);
/* copy buffer to avoid problems with circular refcounts */
buf = gst_buffer_copy (buf);
/* again, for good measure */
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
gst_value_set_buffer (&value, buf);
gst_buffer_unref (buf);
gst_value_array_append_value (array_val, &value);
g_value_unset (&value);
}
#define HDR_TYPE_STREAMINFO 0
#define HDR_TYPE_VORBISCOMMENT 4
static void
gst_flac_enc_process_stream_headers (GstFlacEnc * enc)
{
GstBuffer *vorbiscomment = NULL;
GstBuffer *streaminfo = NULL;
GstBuffer *marker = NULL;
GValue array = { 0, };
GstCaps *caps;
GList *l;
caps = gst_caps_new_simple ("audio/x-flac",
"channels", G_TYPE_INT, enc->channels,
"rate", G_TYPE_INT, enc->sample_rate, NULL);
for (l = enc->headers; l != NULL; l = l->next) {
const guint8 *data;
guint size;
/* mark buffers so oggmux will ignore them if it already muxed the
* header buffers from the streamheaders field in the caps */
l->data = gst_buffer_make_metadata_writable (GST_BUFFER (l->data));
GST_BUFFER_FLAG_SET (GST_BUFFER (l->data), GST_BUFFER_FLAG_IN_CAPS);
data = GST_BUFFER_DATA (GST_BUFFER_CAST (l->data));
size = GST_BUFFER_SIZE (GST_BUFFER_CAST (l->data));
/* find initial 4-byte marker which we need to skip later on */
if (size == 4 && memcmp (data, "fLaC", 4) == 0) {
marker = GST_BUFFER_CAST (l->data);
} else if (size > 1 && (data[0] & 0x7f) == HDR_TYPE_STREAMINFO) {
streaminfo = GST_BUFFER_CAST (l->data);
} else if (size > 1 && (data[0] & 0x7f) == HDR_TYPE_VORBISCOMMENT) {
vorbiscomment = GST_BUFFER_CAST (l->data);
}
}
if (marker == NULL || streaminfo == NULL || vorbiscomment == NULL) {
GST_WARNING_OBJECT (enc, "missing header %p %p %p, muxing into container "
"formats may be broken", marker, streaminfo, vorbiscomment);
goto push_headers;
}
g_value_init (&array, GST_TYPE_ARRAY);
/* add marker including STREAMINFO header */
{
GstBuffer *buf;
guint16 num;
/* minus one for the marker that is merged with streaminfo here */
num = g_list_length (enc->headers) - 1;
buf = gst_buffer_new_and_alloc (13 + GST_BUFFER_SIZE (streaminfo));
GST_BUFFER_DATA (buf)[0] = 0x7f;
memcpy (GST_BUFFER_DATA (buf) + 1, "FLAC", 4);
GST_BUFFER_DATA (buf)[5] = 0x01; /* mapping version major */
GST_BUFFER_DATA (buf)[6] = 0x00; /* mapping version minor */
GST_BUFFER_DATA (buf)[7] = (num & 0xFF00) >> 8;
GST_BUFFER_DATA (buf)[8] = (num & 0x00FF) >> 0;
memcpy (GST_BUFFER_DATA (buf) + 9, "fLaC", 4);
memcpy (GST_BUFFER_DATA (buf) + 13, GST_BUFFER_DATA (streaminfo),
GST_BUFFER_SIZE (streaminfo));
notgst_value_array_append_buffer (&array, buf);
gst_buffer_unref (buf);
}
/* add VORBISCOMMENT header */
notgst_value_array_append_buffer (&array, vorbiscomment);
/* add other headers, if there are any */
for (l = enc->headers; l != NULL; l = l->next) {
if (GST_BUFFER_CAST (l->data) != marker &&
GST_BUFFER_CAST (l->data) != streaminfo &&
GST_BUFFER_CAST (l->data) != vorbiscomment) {
notgst_value_array_append_buffer (&array, GST_BUFFER_CAST (l->data));
}
}
gst_structure_set_value (gst_caps_get_structure (caps, 0),
"streamheader", &array);
g_value_unset (&array);
push_headers:
gst_pad_set_caps (enc->srcpad, caps);
/* push header buffers; update caps, so when we push the first buffer the
* negotiated caps will change to caps that include the streamheader field */
for (l = enc->headers; l != NULL; l = l->next) {
GstBuffer *buf;
buf = GST_BUFFER (l->data);
gst_buffer_set_caps (buf, caps);
GST_LOG ("Pushing header buffer, size %u bytes", GST_BUFFER_SIZE (buf));
(void) gst_pad_push (enc->srcpad, buf);
l->data = NULL;
}
g_list_free (enc->headers);
enc->headers = NULL;
gst_caps_unref (caps);
}
#ifdef LEGACY_FLAC
static FLAC__StreamEncoderWriteStatus
gst_flac_enc_write_callback (const FLAC__SeekableStreamEncoder * encoder,
const FLAC__byte buffer[], unsigned bytes,
unsigned samples, unsigned current_frame, void *client_data)
#else
static FLAC__StreamEncoderWriteStatus
gst_flac_enc_write_callback (const FLAC__StreamEncoder * encoder,
const FLAC__byte buffer[], size_t bytes,
unsigned samples, unsigned current_frame, void *client_data)
#endif
{
GstFlowReturn ret = GST_FLOW_OK;
GstFlacEnc *flacenc;
GstBuffer *outbuf;
flacenc = GST_FLAC_ENC (client_data);
if (flacenc->stopped)
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
outbuf = gst_buffer_new_and_alloc (bytes);
memcpy (GST_BUFFER_DATA (outbuf), buffer, bytes);
if (samples > 0 && flacenc->samples_written != (guint64) - 1) {
guint64 granulepos;
GST_BUFFER_TIMESTAMP (outbuf) = flacenc->start_ts +
GST_FRAMES_TO_CLOCK_TIME (flacenc->samples_written,
flacenc->sample_rate);
GST_BUFFER_DURATION (outbuf) =
GST_FRAMES_TO_CLOCK_TIME (samples, flacenc->sample_rate);
/* offset_end = granulepos for ogg muxer */
granulepos =
flacenc->granulepos_offset + flacenc->samples_written + samples;
GST_BUFFER_OFFSET_END (outbuf) = granulepos;
/* offset = timestamp corresponding to granulepos for ogg muxer
* (see vorbisenc for a much more elaborate version of this) */
GST_BUFFER_OFFSET (outbuf) =
GST_FRAMES_TO_CLOCK_TIME (granulepos, flacenc->sample_rate);
} else {
GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
GST_BUFFER_OFFSET (outbuf) =
flacenc->samples_written * flacenc->width * flacenc->channels;
GST_BUFFER_OFFSET_END (outbuf) = 0;
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_IN_CAPS);
}
/* we assume libflac passes us stuff neatly framed */
if (!flacenc->got_headers) {
if (samples == 0) {
GST_DEBUG_OBJECT (flacenc, "Got header, queueing (%u bytes)",
(guint) bytes);
flacenc->headers = g_list_append (flacenc->headers, outbuf);
/* note: it's important that we increase our byte offset */
goto out;
} else {
GST_INFO_OBJECT (flacenc, "Non-header packet, we have all headers now");
gst_flac_enc_process_stream_headers (flacenc);
flacenc->got_headers = TRUE;
}
} else if (flacenc->got_headers && samples == 0) {
GST_WARNING_OBJECT (flacenc, "Got header packet after data packets");
}
GST_LOG ("Pushing buffer: ts=%" GST_TIME_FORMAT ", samples=%u, size=%u, "
"pos=%" G_GUINT64_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
samples, (guint) bytes, flacenc->offset);
gst_buffer_set_caps (outbuf, GST_PAD_CAPS (flacenc->srcpad));
ret = gst_pad_push (flacenc->srcpad, outbuf);
if (ret != GST_FLOW_OK)
GST_DEBUG_OBJECT (flacenc, "flow: %s", gst_flow_get_name (ret));
flacenc->last_flow = ret;
out:
flacenc->offset += bytes;
flacenc->samples_written += samples;
if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED)
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}
#ifdef LEGACY_FLAC
static FLAC__SeekableStreamEncoderTellStatus
gst_flac_enc_tell_callback (const FLAC__SeekableStreamEncoder * encoder,
FLAC__uint64 * absolute_byte_offset, void *client_data)
#else
static FLAC__StreamEncoderTellStatus
gst_flac_enc_tell_callback (const FLAC__StreamEncoder * encoder,
FLAC__uint64 * absolute_byte_offset, void *client_data)
#endif
{
GstFlacEnc *flacenc = GST_FLAC_ENC (client_data);
*absolute_byte_offset = flacenc->offset;
#ifdef LEGACY_FLAC
return FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK;
#else
return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
#endif
}
static gboolean
gst_flac_enc_sink_event (GstPad * pad, GstEvent * event)
{
GstFlacEnc *flacenc;
GstTagList *taglist;
gboolean ret = TRUE;
flacenc = GST_FLAC_ENC (gst_pad_get_parent (pad));
GST_DEBUG ("Received %s event on sinkpad", GST_EVENT_TYPE_NAME (event));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_NEWSEGMENT:{
GstFormat format;
gint64 start, stream_time;
if (flacenc->offset == 0) {
gst_event_parse_new_segment (event, NULL, NULL, &format, &start, NULL,
&stream_time);
} else {
start = -1;
}
if (start != 0) {
if (flacenc->offset > 0)
GST_DEBUG ("Not handling mid-stream newsegment event");
else
GST_DEBUG ("Not handling newsegment event with non-zero start");
} else {
GstEvent *e = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
0, -1, 0);
ret = gst_pad_push_event (flacenc->srcpad, e);
}
if (stream_time != 0) {
GST_DEBUG ("Not handling non-zero stream time");
}
gst_event_unref (event);
/* don't push it downstream, we'll generate our own via seek to 0 */
break;
}
case GST_EVENT_EOS:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_finish (flacenc->encoder);
#else
FLAC__stream_encoder_finish (flacenc->encoder);
#endif
ret = gst_pad_event_default (pad, event);
break;
case GST_EVENT_TAG:
if (flacenc->tags) {
gst_event_parse_tag (event, &taglist);
gst_tag_list_insert (flacenc->tags, taglist, GST_TAG_MERGE_REPLACE);
} else {
g_assert_not_reached ();
}
ret = gst_pad_event_default (pad, event);
break;
default:
ret = gst_pad_event_default (pad, event);
break;
}
gst_object_unref (flacenc);
return ret;
}
static gboolean
gst_flac_enc_check_discont (GstFlacEnc * flacenc, GstClockTime expected,
GstClockTime timestamp)
{
guint allowed_diff = GST_SECOND / flacenc->sample_rate / 2;
if ((timestamp + allowed_diff < expected)
|| (timestamp > expected + allowed_diff)) {
GST_ELEMENT_WARNING (flacenc, STREAM, FORMAT, (NULL),
("Stream discontinuity detected (wanted %" GST_TIME_FORMAT " got %"
GST_TIME_FORMAT "). The output will have wrong timestamps,"
" consider using audiorate to handle discontinuities",
GST_TIME_ARGS (expected), GST_TIME_ARGS (timestamp)));
return TRUE;
}
/* TODO: Do something to handle discontinuities in the stream. The FLAC encoder
* unfortunately doesn't have any way to flush it's internal buffers */
return FALSE;
}
static GstFlowReturn
gst_flac_enc_chain (GstPad * pad, GstBuffer * buffer)
{
GstFlacEnc *flacenc;
FLAC__int32 *data;
gulong insize;
gint samples, width;
gulong i;
FLAC__bool res;
flacenc = GST_FLAC_ENC (GST_PAD_PARENT (pad));
/* make sure setcaps has been called and the encoder is set up */
if (G_UNLIKELY (flacenc->depth == 0))
return GST_FLOW_NOT_NEGOTIATED;
width = flacenc->width;
/* Save the timestamp of the first buffer. This will be later
* used as offset for all following buffers */
if (flacenc->start_ts == GST_CLOCK_TIME_NONE) {
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
flacenc->start_ts = GST_BUFFER_TIMESTAMP (buffer);
flacenc->granulepos_offset = gst_util_uint64_scale
(GST_BUFFER_TIMESTAMP (buffer), flacenc->sample_rate, GST_SECOND);
} else {
flacenc->start_ts = 0;
flacenc->granulepos_offset = 0;
}
}
/* Check if we have a continous stream, if not drop some samples or the buffer or
* insert some silence samples */
if (flacenc->next_ts != GST_CLOCK_TIME_NONE
&& GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
gst_flac_enc_check_discont (flacenc, flacenc->next_ts,
GST_BUFFER_TIMESTAMP (buffer));
}
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)
&& GST_BUFFER_DURATION_IS_VALID (buffer))
flacenc->next_ts =
GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
else
flacenc->next_ts = GST_CLOCK_TIME_NONE;
insize = GST_BUFFER_SIZE (buffer);
samples = insize / (width >> 3);
data = g_malloc (samples * sizeof (FLAC__int32));
if (width == 8) {
gint8 *indata = (gint8 *) GST_BUFFER_DATA (buffer);
for (i = 0; i < samples; i++)
data[i] = (FLAC__int32) indata[i];
} else if (width == 16) {
gint16 *indata = (gint16 *) GST_BUFFER_DATA (buffer);
for (i = 0; i < samples; i++)
data[i] = (FLAC__int32) indata[i];
} else if (width == 32) {
gint32 *indata = (gint32 *) GST_BUFFER_DATA (buffer);
for (i = 0; i < samples; i++)
data[i] = (FLAC__int32) indata[i];
} else {
g_assert_not_reached ();
}
gst_buffer_unref (buffer);
#ifdef LEGACY_FLAC
res = FLAC__seekable_stream_encoder_process_interleaved (flacenc->encoder,
(const FLAC__int32 *) data, samples / flacenc->channels);
#else
res = FLAC__stream_encoder_process_interleaved (flacenc->encoder,
(const FLAC__int32 *) data, samples / flacenc->channels);
#endif
g_free (data);
if (!res) {
if (flacenc->last_flow == GST_FLOW_OK)
return GST_FLOW_ERROR;
else
return flacenc->last_flow;
}
return GST_FLOW_OK;
}
static void
gst_flac_enc_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstFlacEnc *this = GST_FLAC_ENC (object);
GST_OBJECT_LOCK (this);
switch (prop_id) {
case PROP_QUALITY:
gst_flac_enc_update_quality (this, g_value_get_enum (value));
break;
case PROP_STREAMABLE_SUBSET:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_streamable_subset (this->encoder,
g_value_get_boolean (value));
#else
FLAC__stream_encoder_set_streamable_subset (this->encoder,
g_value_get_boolean (value));
#endif
break;
case PROP_MID_SIDE_STEREO:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_do_mid_side_stereo (this->encoder,
g_value_get_boolean (value));
#else
FLAC__stream_encoder_set_do_mid_side_stereo (this->encoder,
g_value_get_boolean (value));
#endif
break;
case PROP_LOOSE_MID_SIDE_STEREO:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_loose_mid_side_stereo (this->encoder,
g_value_get_boolean (value));
#else
FLAC__stream_encoder_set_loose_mid_side_stereo (this->encoder,
g_value_get_boolean (value));
#endif
break;
case PROP_BLOCKSIZE:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_blocksize (this->encoder,
g_value_get_uint (value));
#else
FLAC__stream_encoder_set_blocksize (this->encoder,
g_value_get_uint (value));
#endif
break;
case PROP_MAX_LPC_ORDER:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_max_lpc_order (this->encoder,
g_value_get_uint (value));
#else
FLAC__stream_encoder_set_max_lpc_order (this->encoder,
g_value_get_uint (value));
#endif
break;
case PROP_QLP_COEFF_PRECISION:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_qlp_coeff_precision (this->encoder,
g_value_get_uint (value));
#else
FLAC__stream_encoder_set_qlp_coeff_precision (this->encoder,
g_value_get_uint (value));
#endif
break;
case PROP_QLP_COEFF_PREC_SEARCH:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search (this->encoder,
g_value_get_boolean (value));
#else
FLAC__stream_encoder_set_do_qlp_coeff_prec_search (this->encoder,
g_value_get_boolean (value));
#endif
break;
case PROP_ESCAPE_CODING:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_do_escape_coding (this->encoder,
g_value_get_boolean (value));
#else
FLAC__stream_encoder_set_do_escape_coding (this->encoder,
g_value_get_boolean (value));
#endif
break;
case PROP_EXHAUSTIVE_MODEL_SEARCH:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_do_exhaustive_model_search
(this->encoder, g_value_get_boolean (value));
#else
FLAC__stream_encoder_set_do_exhaustive_model_search (this->encoder,
g_value_get_boolean (value));
#endif
break;
case PROP_MIN_RESIDUAL_PARTITION_ORDER:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_min_residual_partition_order
(this->encoder, g_value_get_uint (value));
#else
FLAC__stream_encoder_set_min_residual_partition_order (this->encoder,
g_value_get_uint (value));
#endif
break;
case PROP_MAX_RESIDUAL_PARTITION_ORDER:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_max_residual_partition_order
(this->encoder, g_value_get_uint (value));
#else
FLAC__stream_encoder_set_max_residual_partition_order (this->encoder,
g_value_get_uint (value));
#endif
break;
case PROP_RICE_PARAMETER_SEARCH_DIST:
#ifdef LEGACY_FLAC
FLAC__seekable_stream_encoder_set_rice_parameter_search_dist
(this->encoder, g_value_get_uint (value));
#else
FLAC__stream_encoder_set_rice_parameter_search_dist (this->encoder,
g_value_get_uint (value));
#endif
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
GST_OBJECT_UNLOCK (this);
}
static void
gst_flac_enc_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstFlacEnc *this = GST_FLAC_ENC (object);
GST_OBJECT_LOCK (this);
switch (prop_id) {
case PROP_QUALITY:
g_value_set_enum (value, this->quality);
break;
case PROP_STREAMABLE_SUBSET:
#ifdef LEGACY_FLAC
g_value_set_boolean (value,
FLAC__seekable_stream_encoder_get_streamable_subset (this->encoder));
#else
g_value_set_boolean (value,
FLAC__stream_encoder_get_streamable_subset (this->encoder));
#endif
break;
case PROP_MID_SIDE_STEREO:
#ifdef LEGACY_FLAC
g_value_set_boolean (value,
FLAC__seekable_stream_encoder_get_do_mid_side_stereo (this->encoder));
#else
g_value_set_boolean (value,
FLAC__stream_encoder_get_do_mid_side_stereo (this->encoder));
#endif
break;
case PROP_LOOSE_MID_SIDE_STEREO:
#ifdef LEGACY_FLAC
g_value_set_boolean (value,
FLAC__seekable_stream_encoder_get_loose_mid_side_stereo
(this->encoder));
#else
g_value_set_boolean (value,
FLAC__stream_encoder_get_loose_mid_side_stereo (this->encoder));
#endif
break;
case PROP_BLOCKSIZE:
#ifdef LEGACY_FLAC
g_value_set_uint (value,
FLAC__seekable_stream_encoder_get_blocksize (this->encoder));
#else
g_value_set_uint (value,
FLAC__stream_encoder_get_blocksize (this->encoder));
#endif
break;
case PROP_MAX_LPC_ORDER:
#ifdef LEGACY_FLAC
g_value_set_uint (value,
FLAC__seekable_stream_encoder_get_max_lpc_order (this->encoder));
#else
g_value_set_uint (value,
FLAC__stream_encoder_get_max_lpc_order (this->encoder));
#endif
break;
case PROP_QLP_COEFF_PRECISION:
#ifdef LEGACY_FLAC
g_value_set_uint (value,
FLAC__seekable_stream_encoder_get_qlp_coeff_precision
(this->encoder));
#else
g_value_set_uint (value,
FLAC__stream_encoder_get_qlp_coeff_precision (this->encoder));
#endif
break;
case PROP_QLP_COEFF_PREC_SEARCH:
#ifdef LEGACY_FLAC
g_value_set_boolean (value,
FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search
(this->encoder));
#else
g_value_set_boolean (value,
FLAC__stream_encoder_get_do_qlp_coeff_prec_search (this->encoder));
#endif
break;
case PROP_ESCAPE_CODING:
#ifdef LEGACY_FLAC
g_value_set_boolean (value,
FLAC__seekable_stream_encoder_get_do_escape_coding (this->encoder));
#else
g_value_set_boolean (value,
FLAC__stream_encoder_get_do_escape_coding (this->encoder));
#endif
break;
case PROP_EXHAUSTIVE_MODEL_SEARCH:
#ifdef LEGACY_FLAC
g_value_set_boolean (value,
FLAC__seekable_stream_encoder_get_do_exhaustive_model_search
(this->encoder));
#else
g_value_set_boolean (value,
FLAC__stream_encoder_get_do_exhaustive_model_search (this->encoder));
#endif
break;
case PROP_MIN_RESIDUAL_PARTITION_ORDER:
#ifdef LEGACY_FLAC
g_value_set_uint (value,
FLAC__seekable_stream_encoder_get_min_residual_partition_order
(this->encoder));
#else
g_value_set_uint (value,
FLAC__stream_encoder_get_min_residual_partition_order
(this->encoder));
#endif
break;
case PROP_MAX_RESIDUAL_PARTITION_ORDER:
#ifdef LEGACY_FLAC
g_value_set_uint (value,
FLAC__seekable_stream_encoder_get_max_residual_partition_order
(this->encoder));
#else
g_value_set_uint (value,
FLAC__stream_encoder_get_max_residual_partition_order
(this->encoder));
#endif
break;
case PROP_RICE_PARAMETER_SEARCH_DIST:
#ifdef LEGACY_FLAC
g_value_set_uint (value,
FLAC__seekable_stream_encoder_get_rice_parameter_search_dist
(this->encoder));
#else
g_value_set_uint (value,
FLAC__stream_encoder_get_rice_parameter_search_dist (this->encoder));
#endif
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
GST_OBJECT_UNLOCK (this);
}
static GstStateChangeReturn
gst_flac_enc_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GstFlacEnc *flacenc = GST_FLAC_ENC (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
case GST_STATE_CHANGE_READY_TO_PAUSED:
flacenc->stopped = FALSE;
flacenc->start_ts = GST_CLOCK_TIME_NONE;
flacenc->next_ts = GST_CLOCK_TIME_NONE;
flacenc->granulepos_offset = 0;
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
#ifdef LEGACY_FLAC
if (FLAC__seekable_stream_encoder_get_state (flacenc->encoder) !=
FLAC__STREAM_ENCODER_UNINITIALIZED) {
flacenc->stopped = TRUE;
FLAC__seekable_stream_encoder_finish (flacenc->encoder);
}
#else
if (FLAC__stream_encoder_get_state (flacenc->encoder) !=
FLAC__STREAM_ENCODER_UNINITIALIZED) {
flacenc->stopped = TRUE;
FLAC__stream_encoder_finish (flacenc->encoder);
}
#endif
flacenc->offset = 0;
flacenc->samples_written = 0;
flacenc->channels = 0;
flacenc->depth = 0;
flacenc->sample_rate = 0;
if (flacenc->meta) {
FLAC__metadata_object_delete (flacenc->meta[0]);
g_free (flacenc->meta);
flacenc->meta = NULL;
}
g_list_foreach (flacenc->headers, (GFunc) gst_mini_object_unref, NULL);
g_list_free (flacenc->headers);
flacenc->headers = NULL;
flacenc->got_headers = FALSE;
flacenc->last_flow = GST_FLOW_OK;
break;
case GST_STATE_CHANGE_READY_TO_NULL:
default:
break;
}
return ret;
}
|