This repository was archived by the owner on May 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.c
More file actions
2590 lines (2279 loc) · 86.1 KB
/
codec.c
File metadata and controls
2590 lines (2279 loc) · 86.1 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
/*****************************************************************************
* codec.c: main encoding/decoding functions
*****************************************************************************
* Copyright (C) 2003-2017 x264vfw project
*
* Authors: Justin Clay
* Laurent Aimar <fenrir@via.ecp.fr>
* Anton Mitrofanov <BugMaster@narod.ru>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include "x264vfw.h"
#include <assert.h>
#include <getopt.h>
#include <utility>
extern "C" const named_str_t x264vfw_preset_table[COUNT_PRESET] =
{
{ "Ultrafast", "ultrafast" },
{ "Superfast", "superfast" },
{ "Veryfast", "veryfast" },
{ "Faster", "faster" },
{ "Fast", "fast" },
{ "Medium", "medium" },
{ "Slow", "slow" },
{ "Slower", "slower" },
{ "Veryslow", "veryslow" },
{ "Placebo", "placebo" }
};
extern "C" const named_str_t x264vfw_tune_table[COUNT_TUNE] =
{
{ "None", "" },
{ "Film", "film" },
{ "Animation", "animation" },
{ "Grain", "grain" },
{ "Still image", "stillimage" },
{ "PSNR", "psnr" },
{ "SSIM", "ssim" }
};
extern "C" const named_str_t x264vfw_profile_table[COUNT_PROFILE] =
{
{ "Auto", "" },
{ "Baseline", "baseline" },
{ "Main", "main" },
{ "High", "high" },
{ "High 10", "high10" },
{ "High 4:2:2", "high422" },
{ "High 4:4:4", "high444" }
};
extern "C" const named_int_t x264vfw_level_table[COUNT_LEVEL] =
{
{ "Auto", -1 },
{ "1.0", 10 },
{ "1b", 9 },
{ "1.1", 11 },
{ "1.2", 12 },
{ "1.3", 13 },
{ "2.0", 20 },
{ "2.1", 21 },
{ "2.2", 22 },
{ "3.0", 30 },
{ "3.1", 31 },
{ "3.2", 32 },
{ "4.0", 40 },
{ "4.1", 41 },
{ "4.2", 42 },
{ "5.0", 50 },
{ "5.1", 51 },
{ "5.2", 52 },
{ "6.0", 60 },
{ "6.1", 61 },
{ "6.2", 62 }
};
extern "C" const named_int_t x264vfw_colorspace_table[COUNT_COLORSPACE] =
{
{ "YUV 4:2:0", CSP_KEEP_I420 },
{ "YUV 4:2:2", CSP_KEEP_I422 },
{ "YUV 4:4:4", CSP_KEEP_I444 },
{ "RGB", CSP_KEEP_RGB },
{ "Gray", CSP_KEEP_GRAY },
};
extern "C" const named_fourcc_t x264vfw_fourcc_table[COUNT_FOURCC] =
{
{ "H264", mmioFOURCC('H','2','6','4') },
{ "h264", mmioFOURCC('h','2','6','4') },
{ "X264", mmioFOURCC('X','2','6','4') },
{ "x264", mmioFOURCC('x','2','6','4') },
{ "AVC1", mmioFOURCC('A','V','C','1') },
{ "avc1", mmioFOURCC('a','v','c','1') },
{ "VSSH", mmioFOURCC('V','S','S','H') }
};
extern "C" const char * const x264vfw_muxer_names[] =
{
"auto",
"raw",
"mkv",
"flv",
"mp4",
"avi",
NULL
};
extern "C" const char * const x264vfw_log_level_names[] = { "none", "error", "warning", "info", "debug", NULL };
extern "C" const char * const x264vfw_range_names[] = { "auto", "tv", "pc", 0 };
typedef enum
{
RANGE_AUTO = -1,
RANGE_TV,
RANGE_PC
} range_enum;
#ifdef _WIN32
/* Functions for dealing with Unicode on Windows. */
FILE *x264vfw_fopen(const char *filename, const char *mode)
{
wchar_t filename_utf16[MAX_PATH];
wchar_t mode_utf16[16];
if (utf8_to_utf16(filename, filename_utf16) && utf8_to_utf16(mode, mode_utf16))
return _wfopen(filename_utf16, mode_utf16);
return NULL;
}
int x264vfw_rename(const char *oldname, const char *newname)
{
wchar_t oldname_utf16[MAX_PATH];
wchar_t newname_utf16[MAX_PATH];
if (utf8_to_utf16(oldname, oldname_utf16) && utf8_to_utf16(newname, newname_utf16))
{
/* POSIX says that rename() removes the destination, but Win32 doesn't. */
_wunlink(newname_utf16);
return _wrename(oldname_utf16, newname_utf16);
}
return -1;
}
int x264vfw_stat(const char *path, x264vfw_struct_stat *buf)
{
wchar_t path_utf16[MAX_PATH];
if (utf8_to_utf16(path, path_utf16))
return _wstati64(path_utf16, buf);
return -1;
}
int x264vfw_is_pipe(const char *path)
{
wchar_t path_utf16[MAX_PATH];
if (utf8_to_utf16(path, path_utf16))
return WaitNamedPipeW(path_utf16, 0);
return 0;
}
#endif
/* Return a valid x264 colorspace or X264VFW_CSP_NONE if it is not supported */
static int get_csp(BITMAPINFOHEADER *hdr)
{
/* For YUV the bitmap is always top-down regardless of the biHeight sign */
int i_vflip = 0;
switch (hdr->biCompression)
{
case FOURCC_I420:
case FOURCC_IYUV:
return X264VFW_CSP_I420 | i_vflip;
case FOURCC_YV12:
return X264VFW_CSP_YV12 | i_vflip;
case FOURCC_YV16:
return X264VFW_CSP_YV16 | i_vflip;
case FOURCC_YV24:
return X264VFW_CSP_YV24 | i_vflip;
case FOURCC_NV12:
return X264VFW_CSP_NV12 | i_vflip;
case FOURCC_YUYV:
case FOURCC_YUY2:
return X264VFW_CSP_YUYV | i_vflip;
case FOURCC_UYVY:
case FOURCC_HDYC:
return X264VFW_CSP_UYVY | i_vflip;
case BI_RGB:
{
i_vflip = hdr->biHeight < 0 ? 0 : X264VFW_CSP_VFLIP;
if (hdr->biBitCount == 24)
return X264VFW_CSP_BGR | i_vflip;
if (hdr->biBitCount == 32)
return X264VFW_CSP_BGRA | i_vflip;
return X264VFW_CSP_NONE;
}
default:
return X264VFW_CSP_NONE;
}
}
static int get_csp(const VDXPixmapLayout* layout)
{
switch (layout->format)
{
case nsVDXPixmap::kPixFormat_YUV420_Planar:
return X264VFW_CSP_YV12;
case nsVDXPixmap::kPixFormat_YUV422_Planar:
return X264VFW_CSP_YV16;
case nsVDXPixmap::kPixFormat_YUV444_Planar:
return X264VFW_CSP_YV24;
case nsVDXPixmap::kPixFormat_XRGB8888:
{
int i_vflip = layout->pitch < 0 ? X264VFW_CSP_VFLIP : 0;
return X264VFW_CSP_BGRA | i_vflip;
}
case nsVDXPixmap::kPixFormat_YUV420_Planar16:
return X264VFW_CSP_YV12 | X264VFW_CSP_HIGH_DEPTH;
case nsVDXPixmap::kPixFormat_YUV422_Planar16:
return X264VFW_CSP_YV16 | X264VFW_CSP_HIGH_DEPTH;
case nsVDXPixmap::kPixFormat_YUV444_Planar16:
return X264VFW_CSP_YV24 | X264VFW_CSP_HIGH_DEPTH;
case nsVDXPixmap::kPixFormat_XRGB64:
{
int i_vflip = layout->pitch < 0 ? X264VFW_CSP_VFLIP : 0;
return X264VFW_CSP_BGRA | i_vflip | X264VFW_CSP_HIGH_DEPTH;
}
case nsVDXPixmap::kPixFormat_Y8:
return X264VFW_CSP_I400;
case nsVDXPixmap::kPixFormat_Y16:
return X264VFW_CSP_I400 | X264VFW_CSP_HIGH_DEPTH;
default:
return X264VFW_CSP_NONE;
}
}
static int get_allowed_csp(CODEC *codec, BITMAPINFOHEADER *hdr, const VDXPixmapLayout* layout=0)
{
int i_csp_keep = codec->config.i_colorspace;
int i_csp = layout ? get_csp(layout) : get_csp(hdr);
switch (i_csp & X264VFW_CSP_MASK)
{
case X264VFW_CSP_I420:
case X264VFW_CSP_YV12:
return (i_csp_keep == CSP_KEEP_I420) ? i_csp : X264VFW_CSP_NONE;
//case X264VFW_CSP_I422:
case X264VFW_CSP_YV16:
return (i_csp_keep == CSP_KEEP_I422) ? i_csp : X264VFW_CSP_NONE;
//case X264VFW_CSP_I444:
case X264VFW_CSP_YV24:
return (i_csp_keep == CSP_KEEP_I444) ? i_csp : X264VFW_CSP_NONE;
case X264VFW_CSP_NV12:
return (i_csp_keep == CSP_KEEP_I420) ? i_csp : X264VFW_CSP_NONE;
case X264VFW_CSP_YUYV:
case X264VFW_CSP_UYVY:
return (i_csp_keep == CSP_KEEP_I422) ? i_csp : X264VFW_CSP_NONE;
case X264VFW_CSP_BGR:
case X264VFW_CSP_BGRA:
return (i_csp_keep == CSP_KEEP_RGB) ? i_csp : X264VFW_CSP_NONE;
case X264VFW_CSP_I400:
return (i_csp_keep == CSP_KEEP_GRAY) ? i_csp : X264VFW_CSP_NONE;
default:
return X264VFW_CSP_NONE;
}
}
static int choose_output_csp(int i_csp, int b_keep_input_csp)
{
i_csp &= X264VFW_CSP_MASK;
switch (i_csp)
{
case X264VFW_CSP_I420:
case X264VFW_CSP_YV12:
return X264_CSP_I420;
//case X264VFW_CSP_I422:
case X264VFW_CSP_YV16:
return b_keep_input_csp ? X264_CSP_I422 : X264_CSP_I420;
//case X264VFW_CSP_I444:
case X264VFW_CSP_YV24:
return b_keep_input_csp ? X264_CSP_I444 : X264_CSP_I420;
case X264VFW_CSP_NV12:
return X264_CSP_NV12;
case X264VFW_CSP_YUYV:
case X264VFW_CSP_UYVY:
return b_keep_input_csp ? X264_CSP_I422 : X264_CSP_I420;
case X264VFW_CSP_BGR:
return b_keep_input_csp ? X264_CSP_BGR : X264_CSP_I420;
case X264VFW_CSP_BGRA:
return b_keep_input_csp ? X264_CSP_BGRA : X264_CSP_I420;
case X264VFW_CSP_I400:
return X264_CSP_I400;
default:
return X264_CSP_I420;
}
}
static int get_output_csp(int i_csp)
{
int hd_flag = (i_csp & X264VFW_CSP_HIGH_DEPTH) ? X264_CSP_HIGH_DEPTH : 0;
i_csp &= X264VFW_CSP_MASK;
switch (i_csp)
{
case X264VFW_CSP_I420:
case X264VFW_CSP_YV12:
return X264_CSP_I420 | hd_flag;
case X264VFW_CSP_YV16:
return X264_CSP_I422 | hd_flag;
case X264VFW_CSP_YV24:
return X264_CSP_I444 | hd_flag;
case X264VFW_CSP_BGRA:
return X264_CSP_BGRA | hd_flag;
case X264VFW_CSP_I400:
return X264_CSP_I400 | hd_flag;
default:
return 0;
}
}
static int x264vfw_img_fill(x264_image_t *img, uint8_t *ptr, int i_csp, int width, int height, const VDXPixmapLayout* layout=0)
{
i_csp &= X264VFW_CSP_MASK;
switch (i_csp)
{
case X264VFW_CSP_I420:
case X264VFW_CSP_YV12:
height = (height + 1) & ~1;
width = (width + 1) & ~1;
img->i_plane = 3;
img->i_stride[0] = width * BPP;
img->i_stride[1] =
img->i_stride[2] = width / 2 * BPP;
img->plane[0] = ptr;
img->plane[1] = img->plane[0] + img->i_stride[0] * height;
img->plane[2] = img->plane[1] + img->i_stride[1] * height / 2;
break;
//case X264VFW_CSP_I422:
case X264VFW_CSP_YV16:
width = (width + 1) & ~1;
img->i_plane = 3;
img->i_stride[0] = width * BPP;
img->i_stride[1] =
img->i_stride[2] = width / 2 * BPP;
img->plane[0] = ptr;
img->plane[1] = img->plane[0] + img->i_stride[0] * height;
img->plane[2] = img->plane[1] + img->i_stride[1] * height;
break;
//case X264VFW_CSP_I444:
case X264VFW_CSP_YV24:
img->i_plane = 3;
img->i_stride[0] =
img->i_stride[1] =
img->i_stride[2] = width * BPP;
img->plane[0] = ptr;
img->plane[1] = img->plane[0] + img->i_stride[0] * height;
img->plane[2] = img->plane[1] + img->i_stride[1] * height;
break;
case X264VFW_CSP_NV12:
height = (height + 1) & ~1;
width = (width + 1) & ~1;
img->i_plane = 2;
img->i_stride[0] =
img->i_stride[1] = width;
img->plane[0] = ptr;
img->plane[1] = img->plane[0] + img->i_stride[0] * height;
break;
case X264VFW_CSP_YUYV:
case X264VFW_CSP_UYVY:
width = (width + 1) & ~1;
img->i_plane = 1;
img->i_stride[0] = 2 * width;
img->plane[0] = ptr;
break;
case X264VFW_CSP_BGR:
img->i_plane = 1;
img->i_stride[0] = ((3 * width + 3) & ~3) * BPP;
img->plane[0] = ptr;
break;
case X264VFW_CSP_BGRA:
img->i_plane = 1;
img->i_stride[0] = 4 * width * BPP;
img->plane[0] = ptr;
break;
case X264VFW_CSP_I400:
img->i_plane = 1;
img->i_stride[0] = width * BPP;
img->plane[0] = ptr;
break;
default:
return -1;
}
if (layout) {
img->i_stride[0] = layout->pitch;
img->i_stride[1] = layout->pitch2;
img->i_stride[2] = layout->pitch3;
img->plane[0] = ptr + layout->data;
img->plane[1] = ptr + layout->data2;
img->plane[2] = ptr + layout->data3;
switch (i_csp)
{
case X264VFW_CSP_YV12:
case X264VFW_CSP_YV16:
case X264VFW_CSP_YV24:
std::swap(img->i_stride[1], img->i_stride[2]);
std::swap(img->plane[1], img->plane[2]);
break;
}
}
return 0;
}
#if defined(HAVE_FFMPEG) && X264VFW_USE_DECODER
static enum AVPixelFormat csp_to_pix_fmt(int i_csp)
{
i_csp &= X264VFW_CSP_MASK;
switch (i_csp)
{
case X264VFW_CSP_I420:
case X264VFW_CSP_YV12:
return AV_PIX_FMT_YUV420P;
//case X264VFW_CSP_I422:
case X264VFW_CSP_YV16:
return AV_PIX_FMT_YUV422P;
//case X264VFW_CSP_I444:
case X264VFW_CSP_YV24:
return AV_PIX_FMT_YUV444P;
case X264VFW_CSP_NV12:
return AV_PIX_FMT_NV12;
case X264VFW_CSP_YUYV:
return AV_PIX_FMT_YUYV422;
case X264VFW_CSP_UYVY:
return AV_PIX_FMT_UYVY422;
case X264VFW_CSP_BGR:
return AV_PIX_FMT_BGR24;
case X264VFW_CSP_BGRA:
return AV_PIX_FMT_BGRA;
default:
return AV_PIX_FMT_NONE;
}
}
static int x264vfw_picture_fill(VFWPicture *picture, uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height)
{
memset(picture, 0, sizeof(VFWPicture));
switch (pix_fmt)
{
case AV_PIX_FMT_YUV420P:
{
int size, size2;
height = (height + 1) & ~1;
width = (width + 1) & ~1;
picture->linesize[0] = width;
picture->linesize[1] =
picture->linesize[2] = width / 2;
size = picture->linesize[0] * height;
size2 = picture->linesize[1] * height / 2;
picture->data[0] = ptr;
picture->data[1] = picture->data[0] + size;
picture->data[2] = picture->data[1] + size2;
return size + 2 * size2;
}
case AV_PIX_FMT_YUV422P:
{
int size, size2;
width = (width + 1) & ~1;
picture->linesize[0] = width;
picture->linesize[1] =
picture->linesize[2] = width / 2;
size = picture->linesize[0] * height;
size2 = picture->linesize[1] * height;
picture->data[0] = ptr;
picture->data[1] = picture->data[0] + size;
picture->data[2] = picture->data[1] + size2;
return size + 2 * size2;
}
case AV_PIX_FMT_YUV444P:
{
int size;
picture->linesize[0] =
picture->linesize[1] =
picture->linesize[2] = width;
size = picture->linesize[0] * height;
picture->data[0] = ptr;
picture->data[1] = picture->data[0] + size;
picture->data[2] = picture->data[1] + size;
return 3 * size;
}
case AV_PIX_FMT_NV12:
{
int size;
height = (height + 1) & ~1;
width = (width + 1) & ~1;
picture->linesize[0] =
picture->linesize[1] = width;
size = picture->linesize[0] * height;
picture->data[0] = ptr;
picture->data[1] = picture->data[0] + size;
return size + size / 2;
}
case AV_PIX_FMT_YUYV422:
case AV_PIX_FMT_UYVY422:
width = (width + 1) & ~1;
picture->linesize[0] = width * 2;
picture->data[0] = ptr;
return picture->linesize[0] * height;
case AV_PIX_FMT_BGR24:
picture->linesize[0] = (width * 3 + 3) & ~3;
picture->data[0] = ptr;
return picture->linesize[0] * height;
case AV_PIX_FMT_BGRA:
picture->linesize[0] = width * 4;
picture->data[0] = ptr;
return picture->linesize[0] * height;
default:
return -1;
}
}
static int x264vfw_picture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
{
VFWPicture dummy_pict;
return x264vfw_picture_fill(&dummy_pict, NULL, pix_fmt, width, height);
}
static int x264vfw_picture_vflip(VFWPicture *picture, enum AVPixelFormat pix_fmt, int width, int height)
{
switch (pix_fmt)
{
// only RGB-formats can need vflip
case AV_PIX_FMT_BGR24:
case AV_PIX_FMT_BGRA:
picture->data[0] += picture->linesize[0] * (height - 1);
picture->linesize[0] = -picture->linesize[0];
break;
default:
return -1;
}
return 0;
}
static void x264vfw_fill_black_frame(uint8_t *ptr, enum AVPixelFormat pix_fmt, int picture_size)
{
switch (pix_fmt)
{
case AV_PIX_FMT_YUV420P:
case AV_PIX_FMT_NV12:
{
int luma_size = picture_size * 2 / 3;
memset(ptr, 0x10, luma_size); /* TV Scale */
memset(ptr + luma_size, 0x80, picture_size - luma_size);
break;
}
case AV_PIX_FMT_YUV422P:
{
int luma_size = picture_size / 2;
memset(ptr, 0x10, luma_size); /* TV Scale */
memset(ptr + luma_size, 0x80, picture_size - luma_size);
break;
}
case AV_PIX_FMT_YUV444P:
{
int luma_size = picture_size / 3;
memset(ptr, 0x10, luma_size); /* TV Scale */
memset(ptr + luma_size, 0x80, picture_size - luma_size);
break;
}
case AV_PIX_FMT_YUYV422:
wmemset((wchar_t *)ptr, 0x8010, picture_size / sizeof(wchar_t)); /* TV Scale */
break;
case AV_PIX_FMT_UYVY422:
wmemset((wchar_t *)ptr, 0x1080, picture_size / sizeof(wchar_t)); /* TV Scale */
break;
default:
memset(ptr, 0x00, picture_size);
break;
}
}
#endif
static int supported_fourcc(DWORD fourcc)
{
int i;
for (i = 0; i < COUNT_FOURCC; i++)
if (fourcc == x264vfw_fourcc_table[i].value)
return TRUE;
return FALSE;
}
/* Return the output format of the compressed data */
LRESULT x264vfw_compress_get_format(CODEC *codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput, VDXPixmapLayout* layout)
{
BITMAPINFOHEADER *outhdr = &lpbiOutput->bmiHeader;
CONFIG *config = &codec->config;
int iWidth;
int iHeight;
int extra_size = 0;
if (codec->b_global_header && codec->h) {
extra_size = (codec->extradata_size+1) & ~1;
}
if (!lpbiOutput)
return sizeof(BITMAPINFOHEADER)+extra_size;
if (layout) {
if (get_csp(layout) == X264VFW_CSP_NONE)
return ICERR_BADFORMAT;
iWidth = layout->w;
iHeight = layout->h;
} else {
BITMAPINFOHEADER *inhdr = &lpbiInput->bmiHeader;
if (get_csp(inhdr) == X264VFW_CSP_NONE)
return ICERR_BADFORMAT;
iWidth = inhdr->biWidth;
iHeight = abs(inhdr->biHeight);
}
if (iWidth <= 0 || iHeight <= 0)
return ICERR_BADFORMAT;
/* We need x2 width/height */
if (iWidth % 2 || iHeight % 2)
return ICERR_BADFORMAT;
memset(outhdr, 0, sizeof(BITMAPINFOHEADER));
outhdr->biSize = sizeof(BITMAPINFOHEADER);
outhdr->biWidth = iWidth;
outhdr->biHeight = iHeight;
outhdr->biPlanes = 1;
outhdr->biBitCount = 24;
/*outhdr->biCompression = config->i_fourcc >= 0 && config->i_fourcc < COUNT_FOURCC
? x264vfw_fourcc_table[config->i_fourcc].value
: x264vfw_fourcc_table[0].value;*/
outhdr->biCompression = x264vfw_fourcc_table[0].value;
outhdr->biSizeImage = x264vfw_compress_get_size(codec, lpbiInput, lpbiOutput);
if (extra_size) {
uint8* p = ((uint8*)outhdr)+sizeof(BITMAPINFOHEADER);
memset(p,0,extra_size);
memcpy(p,codec->extradata,codec->extradata_size);
}
return ICERR_OK;
}
/* Return the maximum number of bytes a single compressed frame can occupy */
LRESULT x264vfw_compress_get_size(CODEC *codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
{
return ((lpbiOutput->bmiHeader.biWidth + 15) & ~15) * ((lpbiOutput->bmiHeader.biHeight + 31) & ~31) * 3 * BPP + 4096;
}
/* Test if the driver can compress a specific input format to a specific output format */
LRESULT x264vfw_compress_query(CODEC *codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput, VDXPixmapLayout* layout)
{
BITMAPINFOHEADER *outhdr = &lpbiOutput->bmiHeader;
int iWidth;
int iHeight;
if (layout) {
if (get_allowed_csp(codec, 0, layout) == X264VFW_CSP_NONE)
return ICERR_BADFORMAT;
iWidth = layout->w;
iHeight = layout->h;
} else {
BITMAPINFOHEADER *inhdr = &lpbiInput->bmiHeader;
if (get_allowed_csp(codec, inhdr) == X264VFW_CSP_NONE)
return ICERR_BADFORMAT;
iWidth = inhdr->biWidth;
iHeight = abs(inhdr->biHeight);
}
if (iWidth <= 0 || iHeight <= 0)
return ICERR_BADFORMAT;
/* We need x2 width/height */
if (iWidth % 2 || iHeight % 2)
return ICERR_BADFORMAT;
if (!lpbiOutput)
return ICERR_OK;
if (iWidth != outhdr->biWidth || iHeight != outhdr->biHeight)
return ICERR_BADFORMAT;
if (!supported_fourcc(outhdr->biCompression))
return ICERR_BADFORMAT;
return ICERR_OK;
}
CODEC* g_codec;
VDLogProc g_logProc;
/* Log functions */
void x264vfw_log_create(CODEC *codec)
{
x264vfw_log_destroy(codec);
if(g_logProc) return;
if (codec->config.i_log_level > X264VFW_LOG_NONE)
codec->hCons = CreateDialogW(x264vfw_hInst, MAKEINTRESOURCEW(IDD_LOG), GetDesktopWindow(), x264vfw_callback_log);
if(codec->hCons) g_codec = codec;
}
void x264vfw_log_destroy(CODEC *codec)
{
if (codec->hCons)
{
DestroyWindow(codec->hCons);
codec->hCons = NULL;
}
codec->b_visible = FALSE;
g_codec = 0;
}
static void x264vfw_log_internal(CODEC *codec, const char *name, int i_level, const char *psz_fmt, va_list arg)
{
char msg[2048];
wchar_t utf16_msg[2048];
const char *s_level;
switch (i_level)
{
case X264_LOG_ERROR:
s_level = "error";
break;
case X264_LOG_WARNING:
s_level = "warning";
break;
case X264_LOG_INFO:
s_level = g_logProc ? 0:"info";
break;
case X264_LOG_DEBUG:
s_level = "debug";
break;
default:
s_level = g_logProc ? 0:"unknown";
break;
}
memset(msg, 0, sizeof(msg));
if (s_level)
snprintf(msg, sizeof(msg) - 1, "%s [%s]: ", name, s_level);
else
snprintf(msg, sizeof(msg) - 1, "%s: ", name);
vsnprintf(msg + strlen(msg), sizeof(msg) - strlen(msg) - 1, psz_fmt, arg);
/* convert UTF-8 to wide chars */
if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, msg, -1, utf16_msg, sizeof(utf16_msg)) &&
!MultiByteToWideChar(CP_ACP, 0, msg, -1, utf16_msg, sizeof(utf16_msg)))
{
#if X264VFW_DEBUG_OUTPUT
OutputDebugString("x264vfw [error]: log msg to unicode conversion failed\n");
OutputDebugString(msg);
#endif
return;
}
#if X264VFW_DEBUG_OUTPUT
OutputDebugStringW(utf16_msg);
#endif
if(g_logProc){
/* Strip final linefeeds (required) */
int idx = wcslen(utf16_msg) - 1;
while (idx >= 0 && (utf16_msg[idx] == L'\n' || utf16_msg[idx] == L'\r'))
utf16_msg[idx--] = 0;
int type = VD_LOG_INFO;
if(i_level==X264_LOG_WARNING) type = VD_LOG_WARNING;
if(i_level==X264_LOG_ERROR) type = VD_LOG_ERROR;
g_logProc(type,utf16_msg);
}
if (codec && codec->hCons)
{
int idx;
HWND h_console;
HDC hdc;
/* Strip final linefeeds (required) */
idx = wcslen(utf16_msg) - 1;
while (idx >= 0 && (utf16_msg[idx] == L'\n' || utf16_msg[idx] == L'\r'))
utf16_msg[idx--] = 0;
if (!codec->b_visible)
{
ShowWindow(codec->hCons, SW_SHOWNORMAL);
codec->b_visible = TRUE;
}
h_console = GetDlgItem(codec->hCons, LOG_IDC_CONSOLE);
idx = SendMessageW(h_console, LB_ADDSTRING, 0, (LPARAM)utf16_msg);
/* Make sure that the last added item is visible (autoscroll) */
if (idx >= 0)
SendMessage(h_console, LB_SETTOPINDEX, (WPARAM)idx, 0);
hdc = GetDC(h_console);
if (hdc)
{
HFONT hfntDefault;
SIZE sz;
if (wcslen(utf16_msg) < ARRAY_ELEMS(utf16_msg) - 1)
wcscat(utf16_msg, L"X"); /* Otherwise item may be clipped */
hfntDefault = (HFONT)SelectObject(hdc, (HFONT)SendMessage(h_console, WM_GETFONT, 0, 0));
GetTextExtentPoint32W(hdc, utf16_msg, wcslen(utf16_msg), &sz);
if (sz.cx > SendMessage(h_console, LB_GETHORIZONTALEXTENT, 0, 0))
SendMessage(h_console, LB_SETHORIZONTALEXTENT, (WPARAM)sz.cx, 0);
SelectObject(hdc, hfntDefault);
ReleaseDC(h_console, hdc);
}
}
}
void x264vfw_cli_log(void *p_private, const char *name, int i_level, const char *psz_fmt, ...)
{
CODEC *codec = (CODEC*)p_private;
va_list arg;
va_start(arg, psz_fmt);
if (codec && (i_level <= codec->config.i_log_level - 1))
{
if (!codec->hCons)
x264vfw_log_create(codec);
x264vfw_log_internal(codec, name, i_level, psz_fmt, arg);
}
else
x264vfw_log_internal(NULL, name, i_level, psz_fmt, arg);
va_end(arg);
}
void x264vfw_log(CODEC *codec, int i_level, const char *psz_fmt, ...)
{
va_list arg;
va_start(arg, psz_fmt);
if (codec && (i_level <= codec->config.i_log_level - 1))
{
if (!codec->hCons)
x264vfw_log_create(codec);
x264vfw_log_internal(codec, "x264", i_level, psz_fmt, arg);
}
else
x264vfw_log_internal(NULL, "x264", i_level, psz_fmt, arg);
va_end(arg);
}
static void x264vfw_log_callback(void *p_private, int i_level, const char *psz_fmt, va_list arg)
{
x264vfw_log_internal((CODEC*)p_private, "x264", i_level, psz_fmt, arg);
}
extern "C" void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
{
x264vfw_log_internal(g_codec, "x264", i_level, psz_fmt, arg);
}
typedef enum
{
OPT_FRAMES = 256,
OPT_SEEK,
OPT_QPFILE,
OPT_THREAD_INPUT,
OPT_QUIET,
OPT_NOPROGRESS,
OPT_LONGHELP,
OPT_PROFILE,
OPT_PRESET,
OPT_TUNE,
OPT_FASTFIRSTPASS,
OPT_SLOWFIRSTPASS,
OPT_FULLHELP,
OPT_FPS,
OPT_MUXER,
OPT_DEMUXER,
OPT_INDEX,
OPT_INTERLACED,
OPT_TCFILE_IN,
OPT_TCFILE_OUT,
OPT_TIMEBASE,
OPT_PULLDOWN,
OPT_LOG_LEVEL,
OPT_DTS_COMPRESSION,
OPT_OUTPUT_CSP,
OPT_RANGE,
#if X264VFW_USE_VIRTUALDUB_HACK
OPT_VD_HACK,
#endif
OPT_NO_OUTPUT
} OptionsOPT;
static char short_options[] = "8A:B:b:f:hI:i:m:o:p:q:r:t:Vvw";
static struct option long_options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "longhelp", no_argument, NULL, OPT_LONGHELP },
{ "fullhelp", no_argument, NULL, OPT_FULLHELP },
{ "version", no_argument, NULL, 'V' },
{ "profile", required_argument, NULL, OPT_PROFILE },
{ "preset", required_argument, NULL, OPT_PRESET },
{ "tune", required_argument, NULL, OPT_TUNE },
{ "fast-firstpass", no_argument, NULL, OPT_FASTFIRSTPASS },
{ "slow-firstpass", no_argument, NULL, OPT_SLOWFIRSTPASS },
{ "bitrate", required_argument, NULL, 'B' },
{ "bframes", required_argument, NULL, 'b' },
{ "b-adapt", required_argument, NULL, 0 },
{ "no-b-adapt", no_argument, NULL, 0 },
{ "b-bias", required_argument, NULL, 0 },
{ "b-pyramid", required_argument, NULL, 0 },
{ "open-gop", no_argument, NULL, 0 },
{ "bluray-compat", no_argument, NULL, 0 },
{ "avcintra-class", required_argument, NULL, 0 },
{ "min-keyint", required_argument, NULL, 'i' },
{ "keyint", required_argument, NULL, 'I' },
{ "intra-refresh", no_argument, NULL, 0 },
{ "scenecut", required_argument, NULL, 0 },
{ "no-scenecut", no_argument, NULL, 0 },
{ "nf", no_argument, NULL, 0 },
{ "no-deblock", no_argument, NULL, 0 },
{ "filter", required_argument, NULL, 0 },