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
|
/*
* encoder.c -- transcode export layer module, implementation.
*
* Copyright (C) Thomas Oestreich - June 2001
* Updated and partially rewritten by
* Francesco Romani - January 2006
* New rotation code written by
* Francesco Romani - May 2006
*
* This file is part of transcode, a video stream processing tool
*
* transcode is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* transcode 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#define SUPPORT_OLD_ENCODER // for now
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "transcode.h"
#include "framebuffer.h"
#include "filter.h"
#include "counter.h"
#include "video_trans.h"
#include "audio_trans.h"
#include "decoder.h"
#include "encoder.h"
#include "frame_threads.h"
#include "libtc/tcframes.h"
#include <stdint.h>
/*************************************************************************/
/* Our data structure forward declaration */
typedef struct tcrotatecontext_ TCRotateContext;
typedef struct tcencoderdata_ TCEncoderData;
/*************************************************************************/
/* private function prototypes */
/* new-style rotation support */
static void tc_rotate_init(TCRotateContext *rotor,
const char *video_base_name,
const char *audio_base_name);
static void tc_rotate_set_frames_limit(TCRotateContext *rotor,
vob_t *vob, uint32_t frames);
static void tc_rotate_set_bytes_limit(TCRotateContext *rotor,
vob_t *vob, uint64_t bytes);
static void tc_rotate_output_name(TCRotateContext *rotor, vob_t *vob);
static int tc_rotate_if_needed_null(TCRotateContext *rotor,
vob_t *vob, uint32_t bytes);
static int tc_rotate_if_needed_by_frames(TCRotateContext *rotor,
vob_t *vob, uint32_t bytes);
static int tc_rotate_if_needed_by_bytes(TCRotateContext *rotor,
vob_t *vob, uint32_t bytes);
/* old-style rotation support is all public, see transcode.h */
/* new-style encoder */
static int encoder_export(TCEncoderData *data, vob_t *vob);
static void encoder_skip(TCEncoderData *data);
static int encoder_flush(TCEncoderData *data);
/* rest of API is already public */
/* old-style encoder */
#ifdef SUPPORT_OLD_ENCODER
static int OLD_tc_export_setup(vob_t *vob,
const char *a_mod, const char *v_mod);
static void OLD_tc_export_shutdown(void);
static int OLD_tc_encoder_init(vob_t *vob);
static int OLD_tc_encoder_open(vob_t *vob);
static int OLD_tc_encoder_close(void);
static int OLD_tc_encoder_stop(void);
static int OLD_encoder_export(TCEncoderData *data, vob_t *vob);
#endif // SUPPORT_OLD_ENCODER
/* misc helpers */
static int need_stop(TCEncoderData *encdata);
static int is_last_frame(TCEncoderData *encdata, int cluster_mode);
static void export_update_formats(vob_t *vob, const TCModuleInfo *vinfo,
const TCModuleInfo *ainfo);
static int alloc_buffers(TCEncoderData *data);
static void free_buffers(TCEncoderData *data);
/*************************************************************************/
/*
* new encoder module design principles
* 1) keep it simple, stupid
* 2) to have more than one encoder doesn't make sense in transcode, so
* 3) new encoder will be monothread, like the old one
*/
/*************************************************************************/
/*************************************************************************/
/*
* new-style output rotation support. Always avalaible, but
* only new code is supposed to use it.
* This code is private since only encoder code it's supposed
* to use it. If this change, this code will be put in a
* separate .c/.h pair.
*
* The tricky part of this code it's mainly the
* vob->{video,audio}_out_file mangling.
* This it's still done mainly for legacy reasons.
* After every rotation, such fields will be updated to point
* not to real initialization data, but to private buffers of (a)
* TCRotateContext strucutre. This can hardly seen as good, and
* should be changed/improved in future releases.
* Anyway, original values of mentioned field isn't lost since it
* will be stored in TCRotateContext.{video,audio}_base_name.
* ------------------------------------------------------------
*/
/*
* TCExportRotate:
* Generic function called after *every* frame was encoded.
* Rotate output file(s) if condition incapsulate in specific
* functions is satisfied.
*
* Parameters:
* rotor: TCRotateContext to use to check condition.
* vob: pointer to vob_t structure to update with new
* export file(s) name after succesfull rotation.
* bytes: total size of byte encoded (Audio + Video) in last
* rencoding loop.
* Return value:
* TC_OK: successful.
* TC_ERROR: error happened and notified using tc_log*().
*
* Of course no error can happen if rotating condition isn't met
* (so no rotation it's supposed to happen).
* Please note that caller code CANNOT know when rotation happens:
* This is a feature, not a bug! Having rotation policy incapsulated
* into this code and rotation machinery transparent to caller
* it's EXACTLY the purpose oft this code! :)
*/
typedef int (*TCExportRotate)(TCRotateContext *rotor, vob_t *vob,
uint32_t bytes);
struct tcrotatecontext_ {
char video_path_buf[PATH_MAX+1];
char audio_path_buf[PATH_MAX+1];
const char *video_base_name;
const char *audio_base_name;
uint32_t chunk_num;
int null_flag;
uint32_t chunk_frames;
uint64_t encoded_bytes;
uint64_t chunk_bytes;
TCExportRotate rotate_if_needed;
};
/*************************************************************************/
/* macro goody for output rotation request */
#define TC_ROTATE_IF_NEEDED(rotor, vob, bytes) \
((rotor)->rotate_if_needed((rotor), (vob), bytes))
/*
* tc_rotate_init:
* initialize a TCRotateContext with given basenames both for
* audio and video output files.
* Uses null rotation function as default rotation function:
* this means that output rotation just never happen.
*
* Parameters:
* rotor: pointer to a TCRotateContext structure to
* initialize.
* video_base_name: basename for main export file (Audio + Video).
* audio_base_name: basename for auxiliary export file
* (separate audio track).
* Return value:
* None.
*/
static void tc_rotate_init(TCRotateContext *rotor,
const char *video_base_name,
const char *audio_base_name)
{
if (rotor != NULL) {
memset(rotor, 0, sizeof(TCRotateContext));
rotor->video_base_name = video_base_name;
rotor->audio_base_name = audio_base_name;
if (video_base_name == NULL || strlen(video_base_name) == 0
|| strcmp(video_base_name, "/dev/null") == 0) {
rotor->null_flag = TC_TRUE;
} else {
rotor->null_flag = TC_FALSE;
strlcpy(rotor->video_path_buf, video_base_name,
sizeof(rotor->video_path_buf));
/*
* FIXME: Yep, this taste like a duplicate.
* The whole *_out_file thing need a deep review,
* but I want to go a little ahead with the whole
* NMS-powered export layer and write a few more
* NMS export modules before to go with this. -- FR
*/
if (audio_base_name == NULL || strlen(audio_base_name) == 0
|| strcmp(audio_base_name, video_base_name) == 0
|| strcmp(audio_base_name, "/dev/null") == 0) {
/*
* DO NOT separate export audio track, use the same
* export file both for audio and for video
*/
strlcpy(rotor->audio_path_buf, video_base_name,
sizeof(rotor->audio_path_buf));
} else {
/* separate audio file */
strlcpy(rotor->audio_path_buf, audio_base_name,
sizeof(rotor->audio_path_buf));
}
}
rotor->rotate_if_needed = tc_rotate_if_needed_null;
}
}
/*
* tc_rotate_set {frames,bytes}_limit:
* setup respecitvely frames and bytes limit for each output chunk.
* When calling this function user ask for rotation, so they also
* directly updates vob.{video,audio}_out_file so even first
* tc_encoder_open() later call will uses names of the right format
* (i.e. with the same layout of second and further chunks).
* This is done in order to avoid any later rename() and disomogeneities
* in output file name as experienced in transcode 1.0.x and before.
*
* Calling this functions multiple times will not hurt anything,
* but only the last limit set will be honoured. In other words,
* it's impossible (yet) to limit output BOTH for frames and for size.
* This may change in future releases.
*
* Parameters:
* rotor: TCRotateContext to set limit on.
* vob: pointer to vob structure to update.
* frames: frame limit for each output chunk.
* bytes: size limit for each output chunk.
* Return value:
* None
* Side effects:
* vob parameter will be updated. Modified fields:
* video_out_file, audio_out_file.
*/
#define PREPARE_OUTPUT_NAME(rotor, vob) \
if ((rotor)->chunk_num == 0) \
tc_rotate_output_name((rotor), (vob))
static void tc_rotate_set_frames_limit(TCRotateContext *rotor,
vob_t *vob, uint32_t frames)
{
if (rotor != NULL && !rotor->null_flag) {
rotor->chunk_frames = frames;
rotor->rotate_if_needed = tc_rotate_if_needed_by_frames;
PREPARE_OUTPUT_NAME(rotor, vob);
}
}
static void tc_rotate_set_bytes_limit(TCRotateContext *rotor,
vob_t *vob, uint64_t bytes)
{
if (rotor != NULL && !rotor->null_flag) {
rotor->chunk_bytes = bytes;
rotor->rotate_if_needed = tc_rotate_if_needed_by_bytes;
PREPARE_OUTPUT_NAME(rotor, vob);
}
}
#undef PREPARE_OUTPUT_NAME
/* helpers ***************************************************************/
/*
* all rotation helpers uses at least if()s as possible, so we must
* drop paranoia here
*/
/*
* tc_rotate_output_name:
* make names of new main/auxiliary output file chunk and updates
* vob fields accordingly.
*
* Parameters:
* rotor: TCRotateContext to use to make new output name(s).
* vob: pointer to vob_t structure to update.
* Return value:
* none.
*/
/* pretty naif, yet. */
/* FIXME: OK, we must deeply review the whole *out-file thing ASAP */
static void tc_rotate_output_name(TCRotateContext *rotor, vob_t *vob)
{
tc_snprintf(rotor->video_path_buf, sizeof(rotor->video_path_buf),
"%s-%03i", rotor->video_base_name, rotor->chunk_num);
tc_snprintf(rotor->audio_path_buf, sizeof(rotor->audio_path_buf),
"%s-%03i", rotor->audio_base_name, rotor->chunk_num);
vob->video_out_file = rotor->video_path_buf;
vob->audio_out_file = rotor->audio_path_buf;
rotor->chunk_num++;
}
/*************************************************************************/
/*
* real rotation policy implementations. Rotate output file(s)
* respectively:
* - never (_null)
* - when encoded frames reach limit (_by_frames)
* - when encoded AND written *bytes* reach limit (_by_bytes).
*
* For details see documentation of TCExportRotate above.
*/
#define ROTATE_UPDATE_COUNTERS(bytes) do { \
rotor->encoded_bytes += (bytes); \
} while (0);
static int tc_rotate_if_needed_null(TCRotateContext *rotor,
vob_t *vob, uint32_t bytes)
{
ROTATE_UPDATE_COUNTERS(bytes);
return TC_OK;
}
#define ROTATE_COMMON_CODE(rotor, vob) do { \
ret = tc_encoder_close(); \
if (ret != TC_OK) { \
tc_log_error(__FILE__, "unable to close output stream"); \
ret = TC_ERROR; \
} else { \
tc_rotate_output_name((rotor), (vob)); \
tc_log_info(__FILE__, "rotating video output stream to %s", \
(rotor)->video_path_buf); \
tc_log_info(__FILE__, "rotating audio output stream to %s", \
(rotor)->audio_path_buf); \
ret = tc_encoder_open((vob)); \
if (ret != TC_OK) { \
tc_log_error(__FILE__, "unable to reopen output stream"); \
ret = TC_ERROR; \
} \
} \
} while (0)
static int tc_rotate_if_needed_by_frames(TCRotateContext *rotor,
vob_t *vob, uint32_t bytes)
{
int ret = TC_OK;
ROTATE_UPDATE_COUNTERS(bytes);
if (tc_get_frames_encoded() >= rotor->chunk_frames) {
ROTATE_COMMON_CODE(rotor, vob);
}
return ret;
}
static int tc_rotate_if_needed_by_bytes(TCRotateContext *rotor,
vob_t *vob, uint32_t bytes)
{
int ret = TC_OK;
ROTATE_UPDATE_COUNTERS(bytes);
if (rotor->encoded_bytes >= rotor->chunk_bytes) {
ROTATE_COMMON_CODE(rotor, vob);
}
return ret;
}
#undef ROTATE_COMMON_CODE
#undef ROTATE_UPDATE_COUNTERS
/*************************************************************************/
/*************************************************************************/
/* real encoder code */
struct tcencoderdata_ {
/* flags, used internally */
int error_flag;
int fill_flag;
/* frame boundaries */
int frame_first; // XXX
int frame_last; // XXX
/* needed by encoder_skip */
int saved_frame_last; // XXX
int this_frame_last; // XXX
int old_frame_last; // XXX
TCEncoderBuffer *buffer;
vframe_list_t *venc_ptr;
aframe_list_t *aenc_ptr;
TCFactory factory;
TCModule vid_mod;
TCModule aud_mod;
TCModule mplex_mod;
TCRotateContext rotor_data;
#ifdef SUPPORT_OLD_ENCODER
transfer_t export_para;
void *ex_a_handle;
void *ex_v_handle;
#endif
};
static TCEncoderData encdata = {
.error_flag = 0,
.fill_flag = 0,
.frame_first = 0,
.frame_last = 0,
.saved_frame_last = 0,
.this_frame_last = 0,
.old_frame_last = 0,
.buffer = NULL,
.venc_ptr = NULL,
.aenc_ptr = NULL,
.factory = NULL,
.vid_mod = NULL,
.aud_mod = NULL,
.mplex_mod = NULL,
/* rotor_data explicitely initialized later */
#ifdef SUPPORT_OLD_ENCODER
.ex_a_handle = NULL,
.ex_v_handle = NULL,
#endif
};
#define RESET_ATTRIBUTES(ptr) do { \
(ptr)->attributes = 0; \
} while (0)
/*
* is_last_frame:
* check if current frame it's supposed to be the last one in
* encoding frame range. Catch all all known special cases
*
* Parameters:
* encdata: fetch current frame id from this structure reference.
* cluster_mode: boolean flag. When in cluster mode we need to take
* some special care.
* Return value:
* !0: current frame is supposed to be the last one
* 0: otherwise
*/
static int is_last_frame(TCEncoderData *encdata, int cluster_mode)
{
int fid = encdata->buffer->frame_id;
if (cluster_mode) {
fid -= tc_get_frames_dropped();
}
if ((encdata->buffer->vptr->attributes & TC_FRAME_IS_END_OF_STREAM
|| encdata->buffer->aptr->attributes & TC_FRAME_IS_END_OF_STREAM)) {
/* `consume' the flag(s) */
encdata->buffer->vptr->attributes &= ~TC_FRAME_IS_END_OF_STREAM;
encdata->buffer->aptr->attributes &= ~TC_FRAME_IS_END_OF_STREAM;
return 1;
}
return (fid == encdata->frame_last);
}
/*
* export_update_formats:
* coerce exported formats to the default ones from the loaded
* encoder modules IF AND ONLY IF user doesn't have requested
* specific ones.
*
* That's a temporary workaround until we have a full-NMS
* export layer.
*
* Parameters:
* vob: pointer to vob_t structure to update.
* vinfo: pointer to TCModuleInfo of video encoder module.
* ainfo: pointer to TCModuleInfo of audio encoder module.
* Return value:
* None
*/
static void export_update_formats(vob_t *vob, const TCModuleInfo *vinfo,
const TCModuleInfo *ainfo)
{
if (vob == NULL || vinfo == NULL || ainfo == NULL) {
/* should never happen */
tc_log_error(__FILE__, "missing export formats references");
}
/*
* OK, that's pretty hackish since export_attributes should
* go away in near future. Neverthless, ex_a_codec features
* a pretty unuseful default (CODEC_MP3), so we don't use
* such default value to safely distinguish between -N given
* or not given.
* And so we must use another flag, and export_attributes are
* the simplest things that work, now/
*/
if (!(vob->export_attributes & TC_EXPORT_ATTRIBUTE_VCODEC)) {
vob->ex_v_codec = vinfo->codecs_out[0];
}
if (!(vob->export_attributes & TC_EXPORT_ATTRIBUTE_ACODEC)) {
vob->ex_a_codec = ainfo->codecs_out[0];
}
}
/* ------------------------------------------------------------
*
* export init
*
* ------------------------------------------------------------*/
int tc_export_init(TCEncoderBuffer *buffer, TCFactory factory)
{
if (buffer == NULL) {
tc_log_error(__FILE__, "missing encoder buffer reference");
return TC_ERROR;
}
encdata.buffer = buffer;
#ifndef SUPPORT_OLD_ENCODER // factory==NULL to signal use of old code
if (factory == NULL) {
tc_log_error(__FILE__, "missing factory reference");
return TC_ERROR;
}
#endif
encdata.factory = factory;
return TC_OK;
}
int tc_export_setup(vob_t *vob,
const char *a_mod, const char *v_mod, const char *m_mod)
{
int match = 0;
const char *mod_name = NULL;
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return OLD_tc_export_setup(vob, a_mod, v_mod);
#endif
if (verbose >= TC_DEBUG) {
tc_log_info(__FILE__, "loading export modules");
}
mod_name = (a_mod == NULL) ?TC_DEFAULT_EXPORT_AUDIO :a_mod;
encdata.aud_mod = tc_new_module(encdata.factory, "encode", mod_name, TC_AUDIO);
if (!encdata.aud_mod) {
tc_log_error(__FILE__, "can't load audio encoder");
return TC_ERROR;
}
mod_name = (v_mod == NULL) ?TC_DEFAULT_EXPORT_VIDEO :v_mod;
encdata.vid_mod = tc_new_module(encdata.factory, "encode", mod_name, TC_VIDEO);
if (!encdata.vid_mod) {
tc_log_error(__FILE__, "can't load video encoder");
return TC_ERROR;
}
mod_name = (m_mod == NULL) ?TC_DEFAULT_EXPORT_MPLEX :m_mod;
encdata.mplex_mod = tc_new_module(encdata.factory, "multiplex", mod_name,
TC_VIDEO|TC_AUDIO);
if (!encdata.mplex_mod) {
tc_log_error(__FILE__, "can't load multiplexor");
return TC_ERROR;
}
export_update_formats(vob, tc_module_get_info(encdata.vid_mod),
tc_module_get_info(encdata.aud_mod));
match = tc_module_match(vob->ex_a_codec,
encdata.aud_mod, encdata.mplex_mod);
if (!match) {
tc_log_error(__FILE__, "audio encoder incompatible "
"with multiplexor");
return TC_ERROR;
}
match = tc_module_match(vob->ex_v_codec,
encdata.vid_mod, encdata.mplex_mod);
if (!match) {
tc_log_error(__FILE__, "video encoder incompatible "
"with multiplexor");
return TC_ERROR;
}
tc_rotate_init(&encdata.rotor_data,
vob->video_out_file, vob->audio_out_file);
return TC_OK;
}
/* ------------------------------------------------------------
*
* export close, unload modules
*
* ------------------------------------------------------------*/
void tc_export_shutdown(void)
{
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return OLD_tc_export_shutdown();
#endif
if (verbose >= TC_DEBUG) {
tc_log_info(__FILE__, "unloading export modules");
}
tc_del_module(encdata.factory, encdata.mplex_mod);
tc_del_module(encdata.factory, encdata.vid_mod);
tc_del_module(encdata.factory, encdata.aud_mod);
}
/* ------------------------------------------------------------
*
* encoder init
*
* ------------------------------------------------------------*/
int tc_encoder_init(vob_t *vob)
{
int ret;
const char *options = NULL;
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return OLD_tc_encoder_init(vob);
#endif
ret = alloc_buffers(&encdata);
if (ret != TC_OK) {
tc_log_error(__FILE__, "can't allocate encoder buffers");
return TC_ERROR;
}
options = (vob->ex_v_string) ?vob->ex_v_string :"";
ret = tc_module_configure(encdata.vid_mod, options, vob);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "video export module error: init failed");
return TC_ERROR;
}
options = (vob->ex_a_string) ?vob->ex_a_string :"";
ret = tc_module_configure(encdata.aud_mod, options, vob);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "audio export module error: init failed");
return TC_ERROR;
}
return TC_OK;
}
/* ------------------------------------------------------------
*
* encoder open
*
* ------------------------------------------------------------*/
int tc_encoder_open(vob_t *vob)
{
int ret;
const char *options = NULL;
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return OLD_tc_encoder_open(vob);
#endif
options = vob->ex_m_string ? vob->ex_m_string : "";
ret = tc_module_configure(encdata.mplex_mod, options, vob);
if (ret == TC_ERROR) {
tc_log_warn(__FILE__, "multiplexor module error: init failed");
return TC_ERROR;
}
// XXX
tc_module_pass_extradata(encdata.vid_mod, encdata.mplex_mod);
return TC_OK;
}
/* ------------------------------------------------------------
*
* encoder close
*
* ------------------------------------------------------------*/
int tc_encoder_close(void)
{
int ret;
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return OLD_tc_encoder_close();
#endif
/* old style code handle flushing in modules, not here */
ret = encoder_flush(&encdata);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "error while closing encoder: flush failed");
return TC_ERROR;
}
ret = tc_module_stop(encdata.mplex_mod);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "multiplexor module error: stop failed");
return TC_ERROR;
}
if(verbose >= TC_DEBUG) {
tc_log_info(__FILE__, "encoder closed");
}
return TC_OK;
}
/* ------------------------------------------------------------
*
* encoder stop
*
* ------------------------------------------------------------*/
int tc_encoder_stop(void)
{
int ret;
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return OLD_tc_encoder_stop();
#endif
ret = tc_module_stop(encdata.vid_mod);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "video export module error: stop failed");
return TC_ERROR;
}
ret = tc_module_stop(encdata.aud_mod);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "audio export module error: stop failed");
return TC_ERROR;
}
free_buffers(&encdata);
if(verbose >= TC_DEBUG) {
tc_log_info(__FILE__, "encoder stopped");
}
return TC_OK;
}
/* ------------------------------------------------------------
*
* encoder main loop helpers
*
* ------------------------------------------------------------*/
static int alloc_buffers(TCEncoderData *data)
{
data->venc_ptr = vframe_alloc_single();
if (data->venc_ptr == NULL) {
goto no_vframe;
}
data->aenc_ptr = aframe_alloc_single();
if (data->aenc_ptr == NULL) {
goto no_aframe;
}
return TC_OK;
no_aframe:
tc_del_video_frame(data->venc_ptr);
no_vframe:
return TC_ERROR;
}
static void free_buffers(TCEncoderData *data)
{
tc_del_video_frame(data->venc_ptr);
tc_del_audio_frame(data->aenc_ptr);
}
/*
* NOTE about counter/condition/mutex handling inside various
* encoder helpers.
*
* Code are still a little bit confusing since things aren't
* updated or used at the same function level.
* Code works, but isn't still well readable.
* We need stil more cleanup and refactoring for future releases.
*/
/*
* dispatch the acquired frames to encoder modules, and adjust frame counters
*/
static int encoder_export(TCEncoderData *data, vob_t *vob)
{
int video_delayed = 0;
int ret;
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return OLD_encoder_export(data, vob);
#endif
/* remove spurious attributes */
RESET_ATTRIBUTES(data->venc_ptr);
RESET_ATTRIBUTES(data->aenc_ptr);
/* step 1: encode video */
ret = tc_module_encode_video(data->vid_mod,
data->buffer->vptr, data->venc_ptr);
if (ret != TC_OK) {
tc_log_error(__FILE__, "error encoding video frame");
data->error_flag = 1;
}
if (data->venc_ptr->attributes & TC_FRAME_IS_DELAYED) {
data->venc_ptr->attributes &= ~TC_FRAME_IS_DELAYED;
video_delayed = 1;
}
/* step 2: encode audio */
if (video_delayed) {
data->buffer->aptr->attributes |= TC_FRAME_IS_CLONED;
tc_log_info(__FILE__, "Delaying audio");
} else {
ret = tc_module_encode_audio(data->aud_mod,
data->buffer->aptr, data->aenc_ptr);
if (ret != TC_OK) {
tc_log_error(__FILE__, "error encoding audio frame");
data->error_flag = 1;
}
}
/* step 3: multiplex and rotate */
// FIXME: Do we really need bytes-written returned from this, or can
// we just return TC_OK/TC_ERROR like other functions? --AC
ret = tc_module_multiplex(data->mplex_mod,
data->venc_ptr, data->aenc_ptr);
if (ret < 0) {
tc_log_error(__FILE__, "error multiplexing encoded frames");
data->error_flag = 1;
}
data->error_flag = TC_ROTATE_IF_NEEDED(&encdata.rotor_data, vob, ret);
/* step 4: show and update stats */
if (tc_progress_meter) {
int last = (data->frame_last == TC_FRAME_LAST)
?(-1) :data->frame_last;
if (!data->fill_flag) {
data->fill_flag = 1;
}
counter_print(1, data->buffer->frame_id, data->frame_first, last);
}
tc_update_frames_encoded(1);
return (data->error_flag) ?TC_ERROR :TC_OK;
}
#define RETURN_IF_NOT_OK(RET, KIND) do { \
if ((RET) != TC_OK) { \
tc_log_error(__FILE__, "error encoding final %s frame", (KIND)); \
return TC_ERROR; \
} \
} while (0)
#define RETURN_IF_MUX_ERROR(BYTES) do { \
if ((BYTES) < 0) { \
tc_log_error(__FILE__, "error multiplexing final audio frame"); \
return TC_ERROR; \
} \
} while (0)
/* DO NOT rotate here, this data belongs to current chunk */
static int encoder_flush(TCEncoderData *data)
{
int ret = TC_ERROR, bytes = 0;
do {
RESET_ATTRIBUTES(data->aenc_ptr);
ret = tc_module_encode_audio(data->aud_mod, NULL, data->aenc_ptr);
RETURN_IF_NOT_OK(ret, "audio");
bytes = tc_module_multiplex(data->mplex_mod, NULL, data->aenc_ptr);
} while (bytes != 0);
RETURN_IF_MUX_ERROR(bytes);
do {
RESET_ATTRIBUTES(data->venc_ptr);
ret = tc_module_encode_video(data->vid_mod, NULL, data->venc_ptr);
RETURN_IF_NOT_OK(ret, "video");
bytes = tc_module_multiplex(data->mplex_mod, data->venc_ptr, NULL);
} while (bytes != 0);
RETURN_IF_MUX_ERROR(bytes);
return TC_OK;
}
void tc_export_rotation_limit_frames(vob_t *vob, uint32_t frames)
{
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return;
#endif
tc_rotate_set_frames_limit(&encdata.rotor_data, vob, frames);
}
void tc_export_rotation_limit_megabytes(vob_t *vob, uint32_t megabytes)
{
#ifdef SUPPORT_OLD_ENCODER
if (!encdata.factory)
return;
#endif
tc_rotate_set_bytes_limit(&encdata.rotor_data,
vob, megabytes * 1024 * 1024);
}
/*************************************************************************/
#ifdef SUPPORT_OLD_ENCODER
#include "dl_loader.h"
/* ------------------------------------------------------------
*
* export init
*
* ------------------------------------------------------------*/
static int OLD_tc_export_setup(vob_t *vob,
const char *a_mod, const char *v_mod)
{
const char *mod_name = NULL;
// load export modules
mod_name = (a_mod == NULL) ?TC_DEFAULT_EXPORT_AUDIO :a_mod;
encdata.ex_a_handle = load_module((char*)mod_name, TC_EXPORT + TC_AUDIO);
if (encdata.ex_a_handle == NULL) {
tc_log_warn(__FILE__, "loading audio export module failed");
return TC_ERROR;
}
mod_name = (v_mod==NULL) ?TC_DEFAULT_EXPORT_VIDEO :v_mod;
encdata.ex_v_handle = load_module((char*)mod_name, TC_EXPORT + TC_VIDEO);
if (encdata.ex_v_handle == NULL) {
tc_log_warn(__FILE__, "loading video export module failed");
return TC_ERROR;
}
encdata.export_para.flag = verbose;
tca_export(TC_EXPORT_NAME, &encdata.export_para, NULL);
if(encdata.export_para.flag != verbose) {
// module returned capability flag
int cc=0;
if (verbose & TC_DEBUG) {
tc_log_info(__FILE__, "audio capability flag 0x%x | 0x%x",
encdata.export_para.flag, vob->im_a_codec);
}
switch (vob->im_a_codec) {
case CODEC_PCM:
cc = (encdata.export_para.flag & TC_CAP_PCM);
break;
case CODEC_AC3:
cc = (encdata.export_para.flag & TC_CAP_AC3);
break;
case CODEC_RAW:
cc = (encdata.export_para.flag & TC_CAP_AUD);
break;
default:
cc = 0;
}
if (cc == 0) {
tc_log_warn(__FILE__, "audio codec not supported by export module");
return TC_ERROR;
}
} else { /* encdata.export_para.flag == verbose */
if (vob->im_a_codec != CODEC_PCM) {
tc_log_warn(__FILE__, "audio codec not supported by export module");
return TC_ERROR;
}
}
encdata.export_para.flag = verbose;
tcv_export(TC_EXPORT_NAME, &encdata.export_para, NULL);
if (encdata.export_para.flag != verbose) {
// module returned capability flag
int cc = 0;
if (verbose & TC_DEBUG) {
tc_log_info(__FILE__, "video capability flag 0x%x | 0x%x",
encdata.export_para.flag, vob->im_v_codec);
}
switch (vob->im_v_codec) {
case CODEC_RGB:
cc = (encdata.export_para.flag & TC_CAP_RGB);
break;
case CODEC_YUV:
cc = (encdata.export_para.flag & TC_CAP_YUV);
break;
case CODEC_YUV422:
cc = (encdata.export_para.flag & TC_CAP_YUV422);
break;
case CODEC_RAW:
case CODEC_RAW_YUV: /* fallthrough */
cc = (encdata.export_para.flag & TC_CAP_VID);
break;
default:
cc = 0;
}
if (cc == 0) {
tc_log_warn(__FILE__, "video codec not supported by export module");
return TC_ERROR;
}
} else { /* encdata.export_para.flag == verbose */
if (vob->im_v_codec != CODEC_RGB) {
tc_log_warn(__FILE__, "video codec not supported by export module");
return TC_ERROR;
}
}
return TC_OK;
}
/* ------------------------------------------------------------
*
* export close, unload modules
*
* ------------------------------------------------------------*/
static void OLD_tc_export_shutdown(void)
{
if (verbose & TC_DEBUG) {
tc_log_info(__FILE__, "unloading export modules");
}
unload_module(encdata.ex_a_handle);
unload_module(encdata.ex_v_handle);
}
/* ------------------------------------------------------------
*
* encoder init
*
* ------------------------------------------------------------*/
static int OLD_tc_encoder_init(vob_t *vob)
{
int ret;
encdata.export_para.flag = TC_VIDEO;
ret = tcv_export(TC_EXPORT_INIT, &encdata.export_para, vob);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "video export module error: init failed");
return TC_ERROR;
}
encdata.export_para.flag = TC_AUDIO;
ret = tca_export(TC_EXPORT_INIT, &encdata.export_para, vob);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "audio export module error: init failed");
return TC_ERROR;
}
return TC_OK;
}
/* ------------------------------------------------------------
*
* encoder open
*
* ------------------------------------------------------------*/
static int OLD_tc_encoder_open(vob_t *vob)
{
int ret;
encdata.export_para.flag = TC_VIDEO;
ret = tcv_export(TC_EXPORT_OPEN, &encdata.export_para, vob);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "video export module error: open failed");
return TC_ERROR;
}
encdata.export_para.flag = TC_AUDIO;
ret = tca_export(TC_EXPORT_OPEN, &encdata.export_para, vob);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "audio export module error: open failed");
return TC_ERROR;
}
return TC_OK;
}
/* ------------------------------------------------------------
*
* encoder close
*
* ------------------------------------------------------------*/
static int OLD_tc_encoder_close(void)
{
/*
* close, errors not fatal.
* flushing handled internally by export modules.
*/
encdata.export_para.flag = TC_AUDIO;
tca_export(TC_EXPORT_CLOSE, &encdata.export_para, NULL);
encdata.export_para.flag = TC_VIDEO;
tcv_export(TC_EXPORT_CLOSE, &encdata.export_para, NULL);
if(verbose & TC_DEBUG) {
tc_log_info(__FILE__, "encoder closed");
}
return TC_OK;
}
/* ------------------------------------------------------------
*
* encoder stop
*
* ------------------------------------------------------------*/
static int OLD_tc_encoder_stop(void)
{
int ret;
encdata.export_para.flag = TC_VIDEO;
ret = tcv_export(TC_EXPORT_STOP, &encdata.export_para, NULL);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "video export module error: stop failed");
return TC_ERROR;
}
encdata.export_para.flag = TC_AUDIO;
ret = tca_export(TC_EXPORT_STOP, &encdata.export_para, NULL);
if (ret != TC_OK) {
tc_log_warn(__FILE__, "audio export module error: stop failed");
return TC_ERROR;
}
return TC_OK;
}
/*
* dispatch the acquired frames to encoder modules
*/
static int OLD_encoder_export(TCEncoderData *data, vob_t *vob)
{
int video_delayed = 0;
/* encode and export video frame */
data->export_para.buffer = data->buffer->vptr->video_buf;
data->export_para.size = data->buffer->vptr->video_size;
data->export_para.attributes = data->buffer->vptr->attributes;
if (data->buffer->vptr->attributes & TC_FRAME_IS_KEYFRAME) {
data->export_para.attributes |= TC_FRAME_IS_KEYFRAME;
}
data->export_para.flag = TC_VIDEO;
if(tcv_export(TC_EXPORT_ENCODE, &data->export_para, vob) < 0) {
tc_log_warn(__FILE__, "error encoding video frame");
data->error_flag = 1;
}
/* maybe clone? */
data->buffer->vptr->attributes = data->export_para.attributes;
if (data->buffer->vptr->attributes & TC_FRAME_IS_DELAYED) {
data->buffer->vptr->attributes &= ~TC_FRAME_IS_DELAYED;
video_delayed = 1;
}
/* encode and export audio frame */
data->export_para.buffer = data->buffer->aptr->audio_buf;
data->export_para.size = data->buffer->aptr->audio_size;
data->export_para.attributes = data->buffer->aptr->attributes;
data->export_para.flag = TC_AUDIO;
if(video_delayed > 0) {
data->buffer->aptr->attributes |= TC_FRAME_IS_CLONED;
tc_log_info(__FILE__, "Delaying audio");
} else {
if (tca_export(TC_EXPORT_ENCODE, &data->export_para, vob) < 0) {
tc_log_warn(__FILE__, "error encoding audio frame");
data->error_flag = 1;
}
/* maybe clone? */
data->buffer->aptr->attributes = data->export_para.attributes;
}
if (tc_progress_meter) {
int last = (data->frame_last == TC_FRAME_LAST) ?(-1) :data->frame_last;
if (!data->fill_flag) {
data->fill_flag = 1;
}
counter_print(1, data->buffer->frame_id, data->frame_first, last);
}
// XXX: _always_ update?
tc_update_frames_encoded(1);
return data->error_flag ? TC_ERROR : TC_OK;
}
/*************************************************************************
* old style rotation support code.
* TEMPORARY merged here until it will be deleted together with
* old encoder code when NMS-powered export layer is ready to switch
* (read: when we have enough encode/multiplexor module to go).
*************************************************************************/
#include "libtc/xio.h"
//-----------------------------------------------------------------
//
// r,R - switch output file
//
//-----------------------------------------------------------------
static int rotate_ctr = 0;
static int rotate_flag = 0;
static char *base = NULL;
void tc_outstream_rotate_request(void)
{
//set flag
rotate_flag = 1;
}
void tc_outstream_rotate(void)
{
char buf[TC_BUF_MAX];
vob_t *vob=tc_get_vob();
if (!rotate_flag)
return;
//reset flag to avoid re-entry
rotate_flag=0;
// do not try to rename /dev/null
if(strcmp(vob->video_out_file, "/dev/null") == 0)
return;
// get current outfile basename
base = tc_strdup(vob->video_out_file);
//check
if (base == NULL)
return;
// close output
if (tc_encoder_close()<0)
tc_error("failed to close output");
// create new filename
tc_snprintf(buf, sizeof(buf), "%s-%03d", base, rotate_ctr++);
//rename old outputfile
if (xio_rename(base, buf) < 0)
tc_error("failed to rename output file");
// reopen output
if (tc_encoder_open(vob) < 0)
tc_error("failed to open output");
tc_log_info(__FILE__, "outfile %s saved to %s", base, buf);
free(base);
}
/*************************************************************************/
#endif // SUPPORT_OLD_ENCODER
/*
* fake encoding, simply adjust frame counters.
*/
static void encoder_skip(TCEncoderData *data)
{
if (tc_progress_meter) {
if (!data->fill_flag) {
data->fill_flag = 1;
}
counter_print(0, data->buffer->frame_id, data->saved_frame_last,
data->frame_first-1);
}
data->buffer->vptr->attributes |= TC_FRAME_IS_OUT_OF_RANGE;
data->buffer->aptr->attributes |= TC_FRAME_IS_OUT_OF_RANGE;
}
static int need_stop(TCEncoderData *encdata)
{
return !tc_running() || !tc_import_status() || encdata->error_flag;
}
/* ------------------------------------------------------------
*
* encoder main loop
*
* ------------------------------------------------------------*/
void tc_encoder_loop(vob_t *vob, int frame_first, int frame_last)
{
int err = 0, eos = 0; /* End Of Stream flag */
if (encdata.this_frame_last != frame_last) {
encdata.old_frame_last = encdata.this_frame_last;
encdata.this_frame_last = frame_last;
}
encdata.error_flag = 0; /* reset */
encdata.frame_first = frame_first;
encdata.frame_last = frame_last;
encdata.saved_frame_last = encdata.old_frame_last;
while (!need_stop(&encdata)) {
/* stop here if pause requested */
tc_pause();
err = encdata.buffer->acquire_video_frame(encdata.buffer, vob);
if (err) {
if (verbose >= TC_DEBUG) {
tc_log_warn(__FILE__, "failed to acquire next raw video frame");
}
break; /* can't acquire video frame */
}
err = encdata.buffer->acquire_audio_frame(encdata.buffer, vob);
if (err) {
if (verbose >= TC_DEBUG) {
tc_log_warn(__FILE__, "failed to acquire next raw audio frame");
}
break; /* can't acquire frame */
}
eos = is_last_frame(&encdata, tc_cluster_mode);
if (eos) {
break;
}
/* check frame id */
if (frame_first <= encdata.buffer->frame_id
&& encdata.buffer->frame_id < frame_last) {
encoder_export(&encdata, vob);
} else { /* frame not in range */
encoder_skip(&encdata);
} /* frame processing loop */
/* release frame buffer memory */
encdata.buffer->dispose_video_frame(encdata.buffer);
encdata.buffer->dispose_audio_frame(encdata.buffer);
}
/* main frame decoding loop */
if (verbose >= TC_CLEANUP) {
if (eos) {
tc_log_info(__FILE__, "encoder last frame finished (%i/%i)",
encdata.buffer->frame_id, encdata.frame_last);
}
tc_log_info(__FILE__, "export terminated - buffer(s) empty");
}
return;
}
/*************************************************************************/
/*
* Local variables:
* c-file-style: "stroustrup"
* c-file-offsets: ((case-label . *) (statement-case-intro . *))
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/
|