-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlibav2yuv.c
More file actions
1511 lines (1216 loc) · 46.3 KB
/
libav2yuv.c
File metadata and controls
1511 lines (1216 loc) · 46.3 KB
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
/*
// libav2yuv
// adapted from
// avcodec_sample.0.4.9.cpp
**<h3>Anything to YUV</h3>
**<p>
**Tested the EDL mode of this program. Discovered what appears to be
**a bug in the libav library in which frames are unexpectedly dropped.
**I have attempted to work around the bug, however it appears to be filetype
**specific. Currently m2v files work accurately. Use this with the
**updated yuvdiag tool to stamp your video for EDL file creation. Or
**use a frame accurate video player.
**</p>
**<p>This is a very simple generic anything to yuv tool, using the
**libav library from ffmpeg. It supports any video file that libav
**supports (which includes WMV3s now!!!) it can be used to replace
**the -vo yuv4mpeg option of mplayer or the -f yuv4mpegpipe option
**of ffmpeg. The tool can also override yuv header values, in the
**case of incorrect aspect ratio or interlacing etc. And will perform
**simple chroma subsampling conversion, if the source file is not in
**the desired chroma subsampling mode. </p>
**
**<p>
**This release now includes an EDL file parser, for joining or editing
**files. This is a first working version is not optimised and may contain bugs.
**I hope to have more testing over the next few months. See sample EDL file
**below.
**</P>
**<p>
**Now uses sws_scaler for chroma resampling rather than the deprecated img_convert,
**So will compile with the newer version of FFMPEG. Also have provision for decode_audio3
**and decode_video2 which has appeared in the latest ffmpeg snapshot.
**</p>
**
**<p>I use this regularily for file conversions, so I hope the code
**is quite mature, with a thorough set of options and help. Any bugs
**should be picked up quickly. </p>
**<p>As I do want this to be a one program fits all for yuv video conversion
**any bugs or issues you have with the software I would like to hear about
**so I can make this the best software.</p>
**
**<h4>EXAMPLE</h4> <p> <tt> libav2yuv strangefile.avi | y4m-yuvfilter
**| ffmpeg -f yuv4mpegpipe -i - -vcodec whatever wantedfile.avi</tt>
**</p>
**
**<h4>HISTORY</h4> <p> <ul>
**<li>9 July 2010. Fixed a major memory leak when concatenating multiple files (eg large number of jpeg frames)
**<li>5 July 2010. Discovered that the SW_SCALER wasn't implemented (or that the changes were lost) Correctly implemented SW_SCALER version. Also added Mono and 420jpeg chroma modes.
**<li>13 May 2009. SWS_SCALER version
**<li>5 Apr 2009. First release of the EDL.
**<li>2 Feb 2009. First release of the PCM writer version (use the -w flag)
**<li>23 Feb 2008. Found a bug that would
**cause frames to be dropped. Determined that the AV decode function
**is decoding data but not saying the frame is finished. I have not
**been able to find why it is exhibiting this behaviour. I have
**worked around this behaviour by outputting the previous frame when
**the frame is not finished. I am not sure if this causes issues
**with non frame based, packet based files (such as mpeg) </li> </ul>
**<p>
**<pre>
**# <filename|tag> <EditMode> <TransitionType>[num] [duration] [srcIn] [srcOut] [recIn] [recOut]
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_01.mpg VA C 0:0:0:0 0:4:9:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_02.mpg VA C 0:0:0:0 0:3:39:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_03.mpg VA C 0:0:0:0 0:2:38:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_04.mpg VA C 0:0:0:0 0:3:9:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_05.mpg VA C 0:0:0:0 0:4:39:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_06.mpg VA C 0:0:0:0 0:4:9:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_07.mpg VA C 0:0:0:0 0:2:9:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_08.mpg VA C 0:0:0:0 0:3:9:0
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_09.mpg VA C 0:0:0:0 0:3:38:15
** /Users/d332027/Movies/TheFaceOfTheEnemy/the_face_of_the_enemy_10.mpg VA C 0:0:0:0 0:5:46:0
**</pre>
**</p>
**<p>
**The fields are:
**<ul>
**<li>Filename, the full or relative path to the movie asset.
**<li>Editmode; Audio, video or both. Use the Audio or video from the movie asset.
**<li>Transition type; Currently C only (cut) may include more in future.
**<li>Source In; Timestamp indicating the first frame to write.
**<li>Source Out; Timestamp indicating the last frame to write. (An in and out of 0:0:0:0 will write 1 frame)
**</ul>
**</p>
**
*/
/* Possible inclusion for EDL
Comments
Comments can appear at the beginning of the EDL file (header) or between the edit lines in the EDL. The first block of comments in the file is defined to be the header comments and they are associated with the EDL as a whole. Subsequent comments in the EDL file are associated with the first edit line that appears after them.
Edit Entries
<filename|tag> <EditMode> <TransitionType>[num] [duration] [srcIn] [srcOut] [recIn] [recOut]
<filename|tag>: Filename or tag value. Filename can be for an MPEG file, Image file, or Image file template. Image file templates use the same pattern matching as for command line glob, and can be used to specify images to encode into MPEG. i.e. /usr/data/images/image*.jpg
<EditMode>: 'V' | 'A' | 'VA' | 'B' | 'v' | 'a' | 'va' | 'b' which equals Video, Audio, Video_Audio edits (note B or b can be used in place of VA or va).
<TransitonType>: 'C' | 'D' | 'E' | 'FI' | 'FO' | 'W' | 'c' | 'd' | 'e' | 'fi' | 'fo' | 'w'. which equals Cut, Dissolve, Effect, FadeIn, FadeOut, Wipe.
[num]: if TransitionType = Wipe, then a wipe number must be given. At the moment only wipe 'W0' and 'W1' are supported.
[duration]: if the TransitionType is not equal to Cut, then an effect duration must be given. Duration is in frames.
[srcIn]: Src in. If no srcIn is given, then it defaults to the first frame of the video or the first frame in the image pattern. If srcIn isn't specified, then srcOut, recIn, recOut can't be specified.
[srcOut]: Src out. If no srcOut is given, then it defaults to the last frame of the video - or last image in the image pattern. if srcOut isn't given, then recIn and recOut can't be specified.
[recIn]: Rec in. If no recIn is given, then it is calculated based on its position in the EDL and the length of its input.
[recOut]: Rec out. If no recOut is given, then it is calculated based on its position in the EDL and the length of its input. first frame of the video.
For srcIn, srcOut, recIn, recOut, the values can be specified as either timecode, frame number, seconds, or mps seconds. i.e.
[tcode | fnum | sec | mps], where:
tcode : SMPTE timecode in hh:mm:ss:ff
fnum : frame number (the first decodable frame in the video is taken to be frame 0).
sec : seconds with 's' suffix (e.g. 5.2s)
mps : seconds with 'mps' suffix (e.g. 5.2mps). This corresponds to the 'seconds' value displayed by Windows MediaPlayer.
*/
#include <yuv4mpeg.h>
#include <mpegconsts.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <regex.h>
#define BYTES_PER_SAMPLE 4
#define PAL "PAL"
#define PAL_WIDE "PAL_WIDE"
#define NTSC "NTSC"
#define NTSC_WIDE "NTSC_WIDE"
/*
struct commandlineopts {
&yuv_interlacing,&yuv_frame_rate,&yuv_aspect, &yuv_ss_mode,&fdOut,
&audioWrite,&search_codec_type,&convert,&stream,avif,&rangeString,&subRange
};
*/
struct edlentry {
char *filename;
char audio;
char video;
char *in;
char *out;
};
// ^([^ /]+) ([AVBavb]|VA|va) (C) ([0-9]*:?[0-9]*:?[0-9]*[;:]?[0-9]+) ([0-9]*:?[0-9]*:?[0-9]*[;:]?[0-9]+)
// going to use the regex library
// 00:00:00;00
// I'm not sure if this code is smaller than my non regex code
// but lessons learnt here will be useful for the EDL parser
int64_t parseTimecodeRE (char *tc, int frn, int frd) {
// char *pattern = "^([0-9][0-9]*)(:)([0-9][0-9]*)(:)([0-9][0-9]*)([:;])([0-9][0-9]*)$";
// char *pattern = "^([0-9]*)(:?)([0-9]*)(:?)([0-9]*)([:;]?)([0-9]+)$";
char *pattern = "^([0-9]*)(^|:)([0-9]*)(^|:)([0-9]*)(^|[:;])([0-9]+).*$";
// char *pattern = "^\([0-9]*\)\(:?\)\([0-9]*\)\(:?\)\([0-9]*\)\([:;]?\)\([0-9]+\)$";
// char *pattern = "^([0-9]+)(:)([0-9]+)(:)([0-9]+)([:;])([0-9]+)$";
regex_t tc_reg;
int h=0,m=0,s=0,f=0,fn;
size_t num=8;
regmatch_t codes[8];
int nummatch;
float fps,frameNumber;
int64_t fn64;
// fprintf (stderr, "REGCOMP %s\n",pattern);
if (regcomp(&tc_reg, pattern, REG_EXTENDED) != 0) {
fprintf (stderr, "REGEX compile failed\n"); // since I know that the REGEX is correct, what else would cause this.
return -1;
}
//fprintf (stderr, "REGEXEC %s\n",tc);
nummatch = regexec(&tc_reg, tc, num, codes, 0 );
//fprintf (stderr, "REGEXEC %s\n",tc);
if ( nummatch != 0) {
fprintf (stderr, "parser: error REGEX match failed\n");
return -1;
}
/*
for (f=0; f<num; f++) {
le =codes[f].rm_eo-codes[f].rm_so;
off = codes[f].rm_so;
fprintf (stderr,"%d: from %lld to %lld (%.*s)\n",f,codes[f].rm_so,codes[f].rm_eo,le,tc+off);
}
*/
if ( 1.0 * frn / frd == 30000.0 / 1001.0) {
// or is this a : ?
if (tc[codes[6].rm_so] == ':') {
fprintf (stderr,"parser: NTSC Drop Code\n");
frn = 30;
frd = 1;
}
}
fps = 1.0 * frn / frd;
// split the string by converting the : into nulls
for (f=2; f < 7; f+=2)
if (codes[f].rm_eo != 0) {
tc[codes[f].rm_so] = '\0';
}
// convert into integers
if (codes[7].rm_eo != 0)
f = atoi(tc+codes[7].rm_so);
if (codes[5].rm_eo != 0)
s = atoi(tc+codes[5].rm_so);
if (codes[3].rm_eo != 0)
m = atoi(tc+codes[3].rm_so);
if (codes[1].rm_eo != 0)
h = atoi(tc+codes[1].rm_so);
regfree( &tc_reg );
if ((h>0 && m>59) || (m>0 && s>59) || (s>0 && f >= fps)) {
fprintf (stderr,"parser error: timecode digit too large\n");
return -1;
}
// why aren't we doing fn64 at this point?
// fprintf (stderr," %d - %d - %d - %d \n",h,m,s,f);
frameNumber = 1.0 * ( h * 3600 + m * 60 + s ) * fps + f;
fn = (frameNumber);
fn64 = fn;
// fprintf (stderr,"parser: framenumber %d == %lld\n",fn,fn64);
return fn64;
}
int splitTimecode(char **s, char **e, char *rs) {
int dashcount = 0;
int dashplace = 0;
int i;
for (i=0; i<strlen(rs); i++) {
if (rs[i] == '-') {
dashcount ++;
dashplace = i;
}
}
if (dashcount == 1) {
*e = rs + dashplace + 1;
*s = rs;
rs[dashplace] = '\0';
return 0;
}
return -1;
}
int parseEDLline (char *line, char **fn, char *audio, char *video, char **in, char **out)
{
regex_t tc_reg;
size_t num=10;
regmatch_t codes[10];
int rc,f;
char *va;
// char *pattern = "^([^ ]+)( +)([AVBavb]|VA|va)( +)(C)( +)([0-9]*:?[0-9]*:?[0-9]*[;:]?[0-9]+)( +)([0-9]*:?[0-9]*:?[0-9]*[;:]?[0-9]+)$";
char *pattern = "^([^ ]+)( +)([AVBavb]|VA|va)( +)(C)( +)([0-9]*:?[0-9]*:?[0-9]*[;:]?[0-9]+)( +)([0-9]*:?[0-9]*:?[0-9]*[;:]?[0-9]+).*$";
if (regcomp(&tc_reg, pattern, REG_EXTENDED) != 0) {
fprintf (stderr, "REGEX compile failed\n");
return -1;
}
*audio = 0;
*video = 0;
*in = 0;
*out = 0;
*fn=0;
// fprintf (stderr, "REGEXEC %s\n",tc);
rc = regexec(&tc_reg, line, num, codes, 0 );
if ( rc != 0) {
fprintf (stderr, "parser: EDL error REGEX match failed\n");
return -1;
}
/*
for (f=0; f<num; f++) {
le =codes[f].rm_eo-codes[f].rm_so;
off = codes[f].rm_so;
fprintf (stderr,"%d: from %lld to %lld (%.*s)\n",f,codes[f].rm_so,codes[f].rm_eo,le,line+off);
}
*/
for (f=2; f <= 8; f+=2)
if (codes[f].rm_eo != 0) {
line[codes[f].rm_so] = '\0';
}
*fn = line+codes[1].rm_so;
*in = line+codes[7].rm_so;
*out = line + codes[9].rm_so;
va = line+codes[3].rm_so;
// fprintf (stderr,"EDITMODE: %s\n",va);
if (!strcmp(va,"VA") || !strcmp(va,"va") || va[0]=='B' || va[0]=='b') {
*audio = 1;
*video = 1;
} else if (va[0]=='V' || va[0]=='v') {
*video = 1;
} else if (va[0]=='A' || va[0]=='a') {
*audio = 1;
}
return 0;
}
int edlcount (FILE *file, int *maxline, int *lines)
{
int c;
int count=0;
*maxline=0;
*lines=0;
flockfile(file); // for optimising the single character reads
while (!feof(file)){
c = getc_unlocked(file);
count++;
if (c==10) {
(*lines)++;
if (count > *maxline) {
*maxline = count;
count=0;
}
}
}
funlockfile(file);
rewind(file);
}
int parseEDL (char *file, struct edlentry **list)
{
FILE *fh;
char *line;
int maxline,lines,count=0;
char *fn,*in,*out;
char ema,emv;
struct edlentry *inner;
fh = fopen(file,"r");
if (fh == NULL) {
mjpeg_error ("Opening EDL file");
return -1;
}
// count active lines
// count active characters
edlcount(fh,&maxline,&lines);
// should sanity check maxline and lines
// malloc edlentry array
// malloc read buffer
line = (char *)malloc(maxline);
if (line == NULL) {
mjpeg_error("Error allocating line memory");
fclose (fh);
return -1;
}
*list = (struct edlentry *)malloc(lines*sizeof(struct edlentry));
inner = *list;
if (line == NULL) {
mjpeg_error("Error allocating edl memory\n");
free(line);
fclose (fh);
return -1;
}
while (fgets(line,maxline,fh) != NULL) {
// parse line
if (parseEDLline (line, &fn, &ema, &emv,&in,&out) == -1) {
mjpeg_error("Error in EDL file line: %d: %s\n",count+1,line);
} else {
// fprintf (stderr,"Parsing line: %d\n",count);
// malloc filename
mjpeg_debug("malloc filename.");
inner[count].filename = (char *)malloc(strlen(fn)+1);
if (inner[count].filename == NULL) {
mjpeg_error("Error allocating edl filename memory");
free(line);
free(list);
fclose(fh);
return -1;
}
// check file readable.
// cannot parse timecode at this point as we have no knowledge of the frame rate.
// parse timecode;
// check in < out
inner[count].in = (char *)malloc(strlen(in)+1);
if (inner[count].in == NULL) {
mjpeg_debug("Error allocating edl timecode memory");
free(line);
// grr memory leak
// free(list.filename);
free(list);
fclose(fh);
return -1;
}
inner[count].out = (char *)malloc(strlen(out)+1);
if (inner[count].out == NULL) {
mjpeg_error("Error allocating edl filename memory.");
free(line);
// free(list.filename);
// free(list.in);
free(list);
fclose(fh);
return -1;
}
// copy values to struct.
strcpy(inner[count].filename,fn);
strcpy(inner[count].in,in);
strcpy(inner[count].out,out);
// fprintf (stderr,"a: %d v: %d\n",ema,emv);
inner[count].audio = ema;
inner[count].video = emv;
count++;
// fprintf (stderr,"End of loop\n");
}
}
return count;
}
void avchromacpy (uint8_t *dst[3], AVFrame *src, y4m_stream_info_t *sinfo)
{
int y,h,w;
int cw,ch;
w = y4m_si_get_plane_width(sinfo,0);
h = y4m_si_get_plane_height(sinfo,0);
cw = y4m_si_get_plane_width(sinfo,1);
ch = y4m_si_get_plane_height(sinfo,1);
//mjpeg_debug ("copy %d bytes to: %x from: %x",w,dst[0]+y*w,(src->data[0])+y*src->linesize[0]);
for (y=0; y<h; y++) {
// mjpeg_debug ("copy %d bytes to: %x from: %x",w,dst[0]+y*w,(src->data[0])+y*src->linesize[0]);
memcpy(dst[0]+y*w,(src->data[0])+y*src->linesize[0],w);
if (y<ch) {
#ifdef DEBUG
mjpeg_debug("copy %d bytes to: %x from: %x",cw,dst[1]+y*cw,(src->data[1])+y*src->linesize[1]);
#endif
memcpy(dst[1]+y*cw,(src->data[1])+y*src->linesize[1],cw);
memcpy(dst[2]+y*cw,(src->data[2])+y*src->linesize[2],cw);
}
}
}
/*
void chromalloc(uint8_t *m[3],y4m_stream_info_t *sinfo)
{
int fs,cfs;
fs = y4m_si_get_plane_length(sinfo,0);
cfs = y4m_si_get_plane_length(sinfo,1);
m[0] = (uint8_t *)malloc( fs );
m[1] = (uint8_t *)malloc( cfs);
m[2] = (uint8_t *)malloc( cfs);
mjpeg_debug("alloc yuv_data: %x,%x,%x",m[0],m[1],m[2]);
}
*/
static void print_usage()
{
fprintf (stderr,
"usage: libav2yuv [-s<stream>] [-Ip|b|t] [-F<rate>] [-A<aspect>] [-S<chroma>] [-o<outputfile] <filename>\n"
"converts any media file recognised by libav to yuv4mpeg stream\n"
"\n"
"\t -w Write a PCM file not a video file\n"
"\t -I<pbt> Force interlace mode overides parameters read from media file\n"
"\t -F<n:d> Force framerate\n"
"\t -f <fmt> Force format type (if incorrectly detected)\n"
"\t -A(<n:d>|PAL|PAL_WIDE|NTSC|NTSC_WIDE) Force aspect ratio\n"
"\t -S<chroma> Force chroma subsampling mode\n"
"\t if the mode in the stream is unsupported will upsample to YUV444\n"
"\t -c Force conversion to chroma mode (requires a chroma mode)\n"
"\t -s select stream other than stream 0\n"
"\t -o<outputfile> write to file rather than stdout\n"
"\t -r [[[HH:]MM:]SS:]FF-[[[HH:]MM:]SS:]FF playout only these frames\n"
"\t -E enable y4m extensions (may be required if source file is not a common format)\n"
"\t -h print this help\n"
);
}
int parseCommandline (int argc, char *argv[],
int *yi,
y4m_ratio_t *yfr,
y4m_ratio_t *ya,
int *ysm,
int *fdOut,
int *aw,
int *sct,
int *con,
int *str,
AVInputFormat *av,
char **rs,
int *sr)
{
int i;
const static char *legal_flags = "EwchI:F:A:S:o:s:f:r:e:v:";
*aw=0;
*sct=AVMEDIA_TYPE_VIDEO;
*yi = Y4M_UNKNOWN;
*ysm = Y4M_UNKNOWN;
*fdOut = 1;
*con = 0;
*str = 0;
*sr = 0;
av = NULL;
// Parse commandline arguments
while ((i = getopt (argc, argv, legal_flags)) != -1) {
switch (i) {
case 'E':
y4m_accept_extensions(1); // manual override
break;
case 'I':
switch (optarg[0]) {
case 'P':
case 'p': *yi = Y4M_ILACE_NONE; break;
case 'T':
case 't': *yi = Y4M_ILACE_TOP_FIRST; break;
case 'B':
case 'b': *yi = Y4M_ILACE_BOTTOM_FIRST; break;
default:
mjpeg_error("Unknown value for interlace: '%c'", optarg[0]);
return -1;
break;
}
break;
case 'F':
if( Y4M_OK != y4m_parse_ratio(yfr, optarg) ) {
mjpeg_error_exit1 ("Syntax for frame rate should be Numerator:Denominator");
return -1;
}
break;
case 'A':
if( Y4M_OK != y4m_parse_ratio(ya, optarg) ) {
if (!strcmp(optarg,PAL)) {
y4m_parse_ratio(ya, "128:117");
} else if (!strcmp(optarg,PAL_WIDE)) {
y4m_parse_ratio(ya, "512:351");
} else if (!strcmp(optarg,NTSC)) {
y4m_parse_ratio(ya, "4320:4739");
} else if (!strcmp(optarg,NTSC_WIDE)) {
y4m_parse_ratio(ya, "5760:4739");
} else {
mjpeg_error("Syntax for aspect ratio should be Numerator:Denominator");
mjpeg_error("Or "PAL", "PAL_WIDE", "NTSC" and "NTSC_WIDE".");
return -1;
}
}
break;
case 'S':
*ysm = y4m_chroma_parse_keyword(optarg);
if (*ysm == Y4M_UNKNOWN) {
mjpeg_error("Unknown subsampling mode option: %s", optarg);
mjpeg_error("Try: 420mpeg2 420jpeg 444 422 411 mono");
return -1;
}
break;
case 'o':
*fdOut = open (optarg,O_CREAT|O_WRONLY,0644);
if (*fdOut == -1) {
mjpeg_error_exit1 ("Cannot open file for writing");
}
break;
case 'w':
*aw=1;
*sct=AVMEDIA_TYPE_AUDIO;
break;
case 'c':
*con = 1;
break;
case 's':
*str = atoi(optarg);
break;
case 'f':
av = av_find_input_format (optarg);
break;
case 'r':
*rs = (char *) malloc (strlen(optarg)+1);
strcpy(*rs,optarg);
// would like to split into 2 parts to bring inline with EDL version
*sr=1;
break;
case 'v':
mjpeg_default_handler_verbosity (atoi (optarg));
break;
case 'e':
case 'h':
case '?':
return -1 ;
break;
}
}
return 0;
}
int open_av_file (AVFormatContext **pfc, char *fn, AVInputFormat *avif, int st, int sct,AVCodecContext **pcc, AVCodec **pCodec)
{
int i,avStream=-1;
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
// Open video file
#if LIBAVFORMAT_VERSION_MAJOR < 53
if(av_open_input_file(pfc, fn, avif, 0, NULL)!=0)
#else
if(avformat_open_input(pfc, fn, avif, NULL)!=0)
#endif
return -1; // Couldn't open file
pFormatCtx = *pfc;
// fprintf (stderr,"av_find_stream_info\n");
// Retrieve stream information
#if LIBAVFORMAT_VERSION_MAJOR < 53
if(av_find_stream_info(pFormatCtx)<0)
#else
if(avformat_find_stream_info(pFormatCtx,NULL)<0)
#endif
return -1; // Couldn't find stream information
// fprintf (stderr,"dump_format\n");
// Dump information about file onto standard error
#if LIBAVFORMAT_VERSION_MAJOR < 53
dump_format(pFormatCtx, 0, fn, 0);
#else
av_dump_format(pFormatCtx, 0, fn, 0);
#endif
// Find the first video stream
// not necessarily a video stream but this is legacy code
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==sct)
{
// DEBUG: print out codec
// fprintf (stderr,"Video Codec ID: %d (%s)\n",pFormatCtx->streams[i]->codec->codec_id ,pFormatCtx->streams[i]->codec->codec_name);
if (avStream == -1 && st == 0) {
// May still be overridden by the -s option
avStream=i;
}
if (st == i) {
avStream=i;
break;
}
}
if(avStream==-1) {
mjpeg_error("open_av_file: could not find an AV stream");
return -1; // Didn't find a video stream
}
// Get a pointer to the codec context for the video stream
*pcc=pFormatCtx->streams[avStream]->codec;
pCodecCtx = *pcc;
// Find the decoder for the video stream
*pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(*pCodec==NULL) {
mjpeg_error("open_av_file: could not find codec");
return -1; // Codec not found
}
// Open codec
#if LIBAVCODEC_VERSION_MAJOR < 53
if(avcodec_open(pCodecCtx, *pCodec)<0) {
#else
if(avcodec_open2(pCodecCtx, *pCodec,NULL)<0) {
#endif
mjpeg_error("open_av_file: could not open codec");
return -1; // Could not open codec
}
#if 0
}
#endif
return avStream;
}
int init_video(y4m_ratio_t *yuv_frame_rate, int stream, AVFormatContext *pFormatCtx, y4m_ratio_t *yuv_aspect,
int *convert, int *yuv_ss_mode, int *convert_mode, y4m_stream_info_t *si, AVFrame **pFrame)
{
// All video related decoding
AVCodecContext *pCodecCtx=pFormatCtx->streams[stream]->codec;
// Read framerate, aspect ratio and chroma subsampling from Codec
if (yuv_frame_rate->d == 0) {
yuv_frame_rate->n = pFormatCtx->streams[stream]->r_frame_rate.num;
yuv_frame_rate->d = pFormatCtx->streams[stream]->r_frame_rate.den;
}
if (yuv_aspect->d == 0) {
yuv_aspect->n = pCodecCtx-> sample_aspect_ratio.num;
yuv_aspect->d = pCodecCtx-> sample_aspect_ratio.den;
}
// 0:0 is an invalid aspect ratio default to 1:1
if (yuv_aspect->d == 0 || yuv_aspect->n == 0 ) {
yuv_aspect->n=1;
yuv_aspect->d=1;
}
if (*convert) {
if (*yuv_ss_mode == Y4M_UNKNOWN) {
mjpeg_warn("init_video: Convert to Unknown Chroma Subsampling mode\n");
print_usage();
return -1;
} else {
switch (*yuv_ss_mode) {
case Y4M_CHROMA_420MPEG2: *convert_mode = PIX_FMT_YUV420P; break;
case Y4M_CHROMA_422: *convert_mode = PIX_FMT_YUV422P; break;
case Y4M_CHROMA_444: *convert_mode = PIX_FMT_YUV444P; break;
case Y4M_CHROMA_411: *convert_mode = PIX_FMT_YUV411P; break;
case Y4M_CHROMA_420JPEG: *convert_mode = PIX_FMT_YUVJ420P; break;
case Y4M_CHROMA_MONO: *convert_mode = PIX_FMT_GRAY8; break;
default:
mjpeg_error("Cannot convert to this chroma mode");
return -1;
break;
}
mjpeg_warn("Enabling non standard YUV4MPEG stream");
y4m_accept_extensions(1);
}
} else if (*yuv_ss_mode == Y4M_UNKNOWN) {
switch (pCodecCtx->pix_fmt) {
case PIX_FMT_YUV420P: *yuv_ss_mode=Y4M_CHROMA_420MPEG2; break;
case PIX_FMT_YUV422P: *yuv_ss_mode=Y4M_CHROMA_422; break;
case PIX_FMT_YUV444P: *yuv_ss_mode=Y4M_CHROMA_444; break;
case PIX_FMT_YUV411P: *yuv_ss_mode=Y4M_CHROMA_411; break;
case PIX_FMT_YUVJ420P: *yuv_ss_mode=Y4M_CHROMA_420JPEG; break;
default:
*yuv_ss_mode=Y4M_CHROMA_444;
*convert_mode = PIX_FMT_YUV444P;
// is there a warning function
mjpeg_warn("Unsupported Chroma mode (%d). Upsampling to YUV444",pCodecCtx->pix_fmt);
// enable advanced yuv stream
mjpeg_info("Enabling non standard YUV4MPEG stream");
y4m_accept_extensions(1);
*convert = 1;
break;
}
}
// I will make the assumption that a previous allocation has the same size frame.
// It's a limitation of the output stream, so putting this limit in the allocation
// shouldn't matter
if (*pFrame == NULL) {
// Allocate video frame
*pFrame=avcodec_alloc_frame();
mjpeg_debug("avmalloc pFrame: %x",*pFrame);
}
// Output YUV format details
// is there some mjpeg_info functions?
mjpeg_info ("YUV Aspect Ratio: %d:%d",yuv_aspect->n,yuv_aspect->d);
mjpeg_info ("YUV frame rate: %d:%d",yuv_frame_rate->n,yuv_frame_rate->d);
mjpeg_info ("YUV Chroma Subsampling: %d",*yuv_ss_mode);
// Set the YUV stream details
// Interlace is handled when the first frame is read.
y4m_si_set_sampleaspect(si, *yuv_aspect);
y4m_si_set_framerate(si, *yuv_frame_rate);
y4m_si_set_chroma(si, *yuv_ss_mode);
return 0;
}
// I give up, I cannot work out why av_write_frame is not working
int manual_write_yuv (uint8_t *m[3], y4m_stream_info_t *sinfo) {
int fs,cfs;
fs = y4m_si_get_plane_length(sinfo,0);
cfs = y4m_si_get_plane_length(sinfo,1);
printf("FRAME\n");
write(1,m[0],fs);
write(1,m[1],cfs);
write(1,m[2],cfs);
return 0;
}
int process_video (AVCodecContext *pCodecCtx, AVFrame *pFrame, AVFrame **pFrame444, AVPacket *packet, uint8_t **buffer,
int *header_written, int *yuv_interlacing, int convert, int convert_mode, y4m_stream_info_t *streaminfo,
uint8_t *yuv_data[3], int fdOut, y4m_frame_info_t *frameinfo, int write, struct SwsContext *img_convert_ctx)
{
int frameFinished=0,numBytes;
int write_error_code;
int bytesDecoded;
// mjpeg_debug ("decode video");
// will this cause dropped frames to be output...?? or simply crash.
while (!frameFinished) {
#if LIBAVCODEC_VERSION_MAJOR < 52
bytesDecoded = avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet->data, packet->size);
#else
bytesDecoded = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, packet);
#endif
// I'm trying to handle broken video files, but It's causing the audio to lose sync with the video.
// Maybe the PTS might help
if (bytesDecoded < 0 )
mjpeg_error ("error decoding frame at PTS: %lld\n", packet->pts);
//
// Did we get a video frame?
// frameFinished does not mean decoder finished, means that the packet can be freed.
#ifdef DEBUGPROCESSVIDEO
mjpeg_debug("frameFinished: %d",frameFinished);
#endif
// Save the frame to disk
// As we don't know interlacing until the first frame
// we wait until the first frame is read before setting the interlace flag
// and outputting the YUV header
// It also appears that some codecs don't set width or height until the first frame either
if (!*header_written) {
if (frameFinished) {
if (*yuv_interlacing == Y4M_UNKNOWN) {
if (pFrame->interlaced_frame) {
if (pFrame->top_field_first) {
*yuv_interlacing = Y4M_ILACE_TOP_FIRST;
} else {
*yuv_interlacing = Y4M_ILACE_BOTTOM_FIRST;
}
} else {
*yuv_interlacing = Y4M_ILACE_NONE;
}
}
if (img_convert_ctx) {
// initialise conversion to different chroma subsampling
if (!*pFrame444) {
*pFrame444=avcodec_alloc_frame();
mjpeg_debug("avmalloc pFrame444: %x",*pFrame444);
}
numBytes=avpicture_get_size(convert_mode, pCodecCtx->width, pCodecCtx->height);
if (*buffer == NULL) {
*buffer=(uint8_t *)malloc(numBytes);
mjpeg_debug("malloc buffer %x",*buffer);
}
avpicture_fill((AVPicture *)*pFrame444, *buffer, convert_mode, pCodecCtx->width, pCodecCtx->height);
}
y4m_si_set_interlace(streaminfo, *yuv_interlacing);
y4m_si_set_width(streaminfo, pCodecCtx->width);
y4m_si_set_height(streaminfo, pCodecCtx->height);
#ifdef DEBUGPROCESSVIDEO
fprintf (stderr,"yuv_data: %x pFrame: %x\nchromalloc\n",yuv_data,pFrame);
#endif
chromalloc(yuv_data,streaminfo);
#ifdef DEBUGPROCESSVIDEO
fprintf (stderr,"yuv_data: %x pFrame: %x\n",yuv_data,pFrame);
#endif
mjpeg_info ("YUV interlace: %d",*yuv_interlacing);
mjpeg_info ("YUV Output Resolution: %dx%d",pCodecCtx->width, pCodecCtx->height);
// Need to work out why this isn't being set earlier
// y4m_accept_extensions(1);
if ((write_error_code = y4m_write_stream_header(fdOut, streaminfo)) != Y4M_OK)
{
// should this be fatal?
// Yes as it will result in a broken yuv4mpeg stream, and may cause downstream filters to crash.
mjpeg_error_exit1("Write header failed: %s", y4m_strerr(write_error_code));
}
*header_written = 1;
}
}
if (*header_written) {
if (frameFinished) {
// appears that if it's not decoded there isn't anything in the ffmpeg buffer
// this can cause seg faults.
if (img_convert_ctx) {
// mjpeg_debug("sws_scale(%x,%x,%lu,%d,%x,%lu).",img_convert_ctx, (*pFrame444)->data, (*pFrame444)->linesize,pCodecCtx->height,pFrame->data, pFrame->linesize);
//sws_scale(img_convert_ctx, (*pFrame444)->data, (*pFrame444)->linesize, 0, pCodecCtx->height, pFrame->data, pFrame->linesize);
sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height,(*pFrame444)->data, (*pFrame444)->linesize);
// mjpeg_debug("after sws_scale(). %d \n",convert_mode);
avchromacpy(yuv_data,*pFrame444,streaminfo);
// manual_write_yuv(yuv_data,streaminfo);
/*
yuv_width[0] = y4m_si_get_plane_width(streaminfo,0);
yuv_width[1] = y4m_si_get_plane_width(streaminfo,1);
yuv_width[2] = y4m_si_get_plane_width(streaminfo,2);
sws_scale(img_convert_ctx, yuv_data, yuv_width, 0, pCodecCtx->height, pFrame->data, pFrame->linesize);
*/
} else {
#ifdef DEBUGPROCESSVIDEO
fprintf (stderr,"yuv_data: %x pFrame: %x\n",yuv_data,pFrame);
#endif
avchromacpy(yuv_data,pFrame,streaminfo);
}
}
} /* frame finished */
if (write && *header_written) {