forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpegvideo_enc.c
4316 lines (3798 loc) · 157 KB
/
mpegvideo_enc.c
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
/*
* The simplest mpeg encoder (well, it was the simplest!)
* Copyright (c) 2000,2001 Fabrice Bellard
* Copyright (c) 2002-2004 Michael Niedermayer <[email protected]>
*
* 4MV & hq & B-frame encoding stuff by Michael Niedermayer <[email protected]>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* The simplest mpeg encoder (well, it was the simplest!).
*/
#include "libavutil/internal.h"
#include "libavutil/intmath.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "dct.h"
#include "dsputil.h"
#include "mpeg12.h"
#include "mpegvideo.h"
#include "h261.h"
#include "h263.h"
#include "mathops.h"
#include "mjpegenc.h"
#include "msmpeg4.h"
#include "faandct.h"
#include "thread.h"
#include "aandcttab.h"
#include "flv.h"
#include "mpeg4video.h"
#include "internal.h"
#include "bytestream.h"
#include <limits.h>
#include "sp5x.h"
static int encode_picture(MpegEncContext *s, int picture_number);
static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
static int sse_mb(MpegEncContext *s);
static void denoise_dct_c(MpegEncContext *s, int16_t *block);
static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
const AVOption ff_mpv_generic_options[] = {
FF_MPV_COMMON_OPTS
{ NULL },
};
void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
uint16_t (*qmat16)[2][64],
const uint16_t *quant_matrix,
int bias, int qmin, int qmax, int intra)
{
int qscale;
int shift = 0;
for (qscale = qmin; qscale <= qmax; qscale++) {
int i;
if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
dsp->fdct == ff_jpeg_fdct_islow_10 ||
dsp->fdct == ff_faandct) {
for (i = 0; i < 64; i++) {
const int j = dsp->idct_permutation[i];
/* 16 <= qscale * quant_matrix[i] <= 7905
* Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
* 19952 <= x <= 249205026
* (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
* 3444240 >= (1 << 36) / (x) >= 275 */
qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
(qscale * quant_matrix[j]));
}
} else if (dsp->fdct == ff_fdct_ifast) {
for (i = 0; i < 64; i++) {
const int j = dsp->idct_permutation[i];
/* 16 <= qscale * quant_matrix[i] <= 7905
* Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
* 19952 <= x <= 249205026
* (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
* 3444240 >= (1 << 36) / (x) >= 275 */
qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
(ff_aanscales[i] * (int64_t)qscale * quant_matrix[j]));
}
} else {
for (i = 0; i < 64; i++) {
const int j = dsp->idct_permutation[i];
/* We can safely suppose that 16 <= quant_matrix[i] <= 255
* Assume x = qscale * quant_matrix[i]
* So 16 <= x <= 7905
* so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
* so 32768 >= (1 << 19) / (x) >= 67 */
qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
(qscale * quant_matrix[j]));
//qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
// (qscale * quant_matrix[i]);
qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
(qscale * quant_matrix[j]);
if (qmat16[qscale][0][i] == 0 ||
qmat16[qscale][0][i] == 128 * 256)
qmat16[qscale][0][i] = 128 * 256 - 1;
qmat16[qscale][1][i] =
ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
qmat16[qscale][0][i]);
}
}
for (i = intra; i < 64; i++) {
int64_t max = 8191;
if (dsp->fdct == ff_fdct_ifast) {
max = (8191LL * ff_aanscales[i]) >> 14;
}
while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
shift++;
}
}
}
if (shift) {
av_log(NULL, AV_LOG_INFO,
"Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
QMAT_SHIFT - shift);
}
}
static inline void update_qscale(MpegEncContext *s)
{
s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
(FF_LAMBDA_SHIFT + 7);
s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
FF_LAMBDA_SHIFT;
}
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
{
int i;
if (matrix) {
put_bits(pb, 1, 1);
for (i = 0; i < 64; i++) {
put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
}
} else
put_bits(pb, 1, 0);
}
/**
* init s->current_picture.qscale_table from s->lambda_table
*/
void ff_init_qscale_tab(MpegEncContext *s)
{
int8_t * const qscale_table = s->current_picture.qscale_table;
int i;
for (i = 0; i < s->mb_num; i++) {
unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
s->avctx->qmax);
}
}
static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst,
const AVFrame *src)
{
dst->pict_type = src->pict_type;
dst->quality = src->quality;
dst->coded_picture_number = src->coded_picture_number;
dst->display_picture_number = src->display_picture_number;
//dst->reference = src->reference;
dst->pts = src->pts;
dst->interlaced_frame = src->interlaced_frame;
dst->top_field_first = src->top_field_first;
}
static void update_duplicate_context_after_me(MpegEncContext *dst,
MpegEncContext *src)
{
#define COPY(a) dst->a= src->a
COPY(pict_type);
COPY(current_picture);
COPY(f_code);
COPY(b_code);
COPY(qscale);
COPY(lambda);
COPY(lambda2);
COPY(picture_in_gop_number);
COPY(gop_picture_number);
COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
COPY(progressive_frame); // FIXME don't set in encode_header
COPY(partitioned_frame); // FIXME don't set in encode_header
#undef COPY
}
/**
* Set the given MpegEncContext to defaults for encoding.
* the changed fields will not depend upon the prior state of the MpegEncContext.
*/
static void MPV_encode_defaults(MpegEncContext *s)
{
int i;
ff_MPV_common_defaults(s);
for (i = -16; i < 16; i++) {
default_fcode_tab[i + MAX_MV] = 1;
}
s->me.mv_penalty = default_mv_penalty;
s->fcode_tab = default_fcode_tab;
}
av_cold int ff_dct_encode_init(MpegEncContext *s) {
if (ARCH_X86)
ff_dct_encode_init_x86(s);
if (!s->dct_quantize)
s->dct_quantize = ff_dct_quantize_c;
if (!s->denoise_dct)
s->denoise_dct = denoise_dct_c;
s->fast_dct_quantize = s->dct_quantize;
if (s->avctx->trellis)
s->dct_quantize = dct_quantize_trellis_c;
return 0;
}
/* init video encoder */
av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
int i;
int chroma_h_shift, chroma_v_shift;
MPV_encode_defaults(s);
switch (avctx->codec_id) {
case AV_CODEC_ID_MPEG2VIDEO:
if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
av_log(avctx, AV_LOG_ERROR,
"only YUV420 and YUV422 are supported\n");
return -1;
}
break;
case AV_CODEC_ID_LJPEG:
if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
avctx->pix_fmt != AV_PIX_FMT_BGR0 &&
avctx->pix_fmt != AV_PIX_FMT_BGRA &&
avctx->pix_fmt != AV_PIX_FMT_BGR24 &&
((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
return -1;
}
break;
case AV_CODEC_ID_MJPEG:
case AV_CODEC_ID_AMV:
if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
return -1;
}
break;
default:
if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
return -1;
}
}
switch (avctx->pix_fmt) {
case AV_PIX_FMT_YUVJ444P:
case AV_PIX_FMT_YUV444P:
s->chroma_format = CHROMA_444;
break;
case AV_PIX_FMT_YUVJ422P:
case AV_PIX_FMT_YUV422P:
s->chroma_format = CHROMA_422;
break;
case AV_PIX_FMT_YUVJ420P:
case AV_PIX_FMT_YUV420P:
default:
s->chroma_format = CHROMA_420;
break;
}
s->bit_rate = avctx->bit_rate;
s->width = avctx->width;
s->height = avctx->height;
if (avctx->gop_size > 600 &&
avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
av_log(avctx, AV_LOG_WARNING,
"keyframe interval too large!, reducing it from %d to %d\n",
avctx->gop_size, 600);
avctx->gop_size = 600;
}
s->gop_size = avctx->gop_size;
s->avctx = avctx;
s->flags = avctx->flags;
s->flags2 = avctx->flags2;
s->max_b_frames = avctx->max_b_frames;
s->codec_id = avctx->codec->id;
s->strict_std_compliance = avctx->strict_std_compliance;
s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
s->mpeg_quant = avctx->mpeg_quant;
s->rtp_mode = !!avctx->rtp_payload_size;
s->intra_dc_precision = avctx->intra_dc_precision;
s->user_specified_pts = AV_NOPTS_VALUE;
if (s->gop_size <= 1) {
s->intra_only = 1;
s->gop_size = 12;
} else {
s->intra_only = 0;
}
s->me_method = avctx->me_method;
/* Fixed QSCALE */
s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
s->adaptive_quant = (s->avctx->lumi_masking ||
s->avctx->dark_masking ||
s->avctx->temporal_cplx_masking ||
s->avctx->spatial_cplx_masking ||
s->avctx->p_masking ||
s->avctx->border_masking ||
(s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
!s->fixed_qscale;
s->loop_filter = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
switch(avctx->codec_id) {
case AV_CODEC_ID_MPEG1VIDEO:
case AV_CODEC_ID_MPEG2VIDEO:
avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112L / 15000000 * 16384;
break;
case AV_CODEC_ID_MPEG4:
case AV_CODEC_ID_MSMPEG4V1:
case AV_CODEC_ID_MSMPEG4V2:
case AV_CODEC_ID_MSMPEG4V3:
if (avctx->rc_max_rate >= 15000000) {
avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000L) * (760-320) / (38400000 - 15000000);
} else if(avctx->rc_max_rate >= 2000000) {
avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000L) * (320- 80) / (15000000 - 2000000);
} else if(avctx->rc_max_rate >= 384000) {
avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000L) * ( 80- 40) / ( 2000000 - 384000);
} else
avctx->rc_buffer_size = 40;
avctx->rc_buffer_size *= 16384;
break;
}
if (avctx->rc_buffer_size) {
av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
}
}
if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
if (avctx->rc_max_rate && !avctx->rc_buffer_size)
return -1;
}
if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
av_log(avctx, AV_LOG_INFO,
"Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
}
if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
return -1;
}
if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
return -1;
}
if (avctx->rc_max_rate &&
avctx->rc_max_rate == avctx->bit_rate &&
avctx->rc_max_rate != avctx->rc_min_rate) {
av_log(avctx, AV_LOG_INFO,
"impossible bitrate constraints, this will fail\n");
}
if (avctx->rc_buffer_size &&
avctx->bit_rate * (int64_t)avctx->time_base.num >
avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
return -1;
}
if (!s->fixed_qscale &&
avctx->bit_rate * av_q2d(avctx->time_base) >
avctx->bit_rate_tolerance) {
av_log(avctx, AV_LOG_ERROR,
"bitrate tolerance too small for bitrate\n");
return -1;
}
if (s->avctx->rc_max_rate &&
s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
(s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
90000LL * (avctx->rc_buffer_size - 1) >
s->avctx->rc_max_rate * 0xFFFFLL) {
av_log(avctx, AV_LOG_INFO,
"Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
"specified vbv buffer is too large for the given bitrate!\n");
}
if ((s->flags & CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
s->codec_id != AV_CODEC_ID_FLV1) {
av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
return -1;
}
if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
av_log(avctx, AV_LOG_ERROR,
"OBMC is only supported with simple mb decision\n");
return -1;
}
if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
return -1;
}
if (s->max_b_frames &&
s->codec_id != AV_CODEC_ID_MPEG4 &&
s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
return -1;
}
if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
s->codec_id == AV_CODEC_ID_H263 ||
s->codec_id == AV_CODEC_ID_H263P) &&
(avctx->sample_aspect_ratio.num > 255 ||
avctx->sample_aspect_ratio.den > 255)) {
av_log(avctx, AV_LOG_WARNING,
"Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
}
if ((s->codec_id == AV_CODEC_ID_H263 ||
s->codec_id == AV_CODEC_ID_H263P) &&
(avctx->width > 2048 ||
avctx->height > 1152 )) {
av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
return -1;
}
if ((s->codec_id == AV_CODEC_ID_H263 ||
s->codec_id == AV_CODEC_ID_H263P) &&
((avctx->width &3) ||
(avctx->height&3) )) {
av_log(avctx, AV_LOG_ERROR, "w/h must be a multiple of 4\n");
return -1;
}
if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
(avctx->width > 4095 ||
avctx->height > 4095 )) {
av_log(avctx, AV_LOG_ERROR, "MPEG-1 does not support resolutions above 4095x4095\n");
return -1;
}
if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
(avctx->width > 16383 ||
avctx->height > 16383 )) {
av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support resolutions above 16383x16383\n");
return -1;
}
if (s->codec_id == AV_CODEC_ID_RV10 &&
(avctx->width &15 ||
avctx->height&15 )) {
av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
return AVERROR(EINVAL);
}
if (s->codec_id == AV_CODEC_ID_RV20 &&
(avctx->width &3 ||
avctx->height&3 )) {
av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
return AVERROR(EINVAL);
}
if ((s->codec_id == AV_CODEC_ID_WMV1 ||
s->codec_id == AV_CODEC_ID_WMV2) &&
avctx->width & 1) {
av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
return -1;
}
if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
return -1;
}
// FIXME mpeg2 uses that too
if (s->mpeg_quant && ( s->codec_id != AV_CODEC_ID_MPEG4
&& s->codec_id != AV_CODEC_ID_MPEG2VIDEO)) {
av_log(avctx, AV_LOG_ERROR,
"mpeg2 style quantization not supported by codec\n");
return -1;
}
if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
return -1;
}
if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
s->avctx->mb_decision != FF_MB_DECISION_RD) {
av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
return -1;
}
if (s->avctx->scenechange_threshold < 1000000000 &&
(s->flags & CODEC_FLAG_CLOSED_GOP)) {
av_log(avctx, AV_LOG_ERROR,
"closed gop with scene change detection are not supported yet, "
"set threshold to 1000000000\n");
return -1;
}
if (s->flags & CODEC_FLAG_LOW_DELAY) {
if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
av_log(avctx, AV_LOG_ERROR,
"low delay forcing is only available for mpeg2\n");
return -1;
}
if (s->max_b_frames != 0) {
av_log(avctx, AV_LOG_ERROR,
"b frames cannot be used with low delay\n");
return -1;
}
}
if (s->q_scale_type == 1) {
if (avctx->qmax > 12) {
av_log(avctx, AV_LOG_ERROR,
"non linear quant only supports qmax <= 12 currently\n");
return -1;
}
}
if (s->avctx->thread_count > 1 &&
s->codec_id != AV_CODEC_ID_MPEG4 &&
s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
s->codec_id != AV_CODEC_ID_MJPEG &&
(s->codec_id != AV_CODEC_ID_H263P)) {
av_log(avctx, AV_LOG_ERROR,
"multi threaded encoding not supported by codec\n");
return -1;
}
if (s->avctx->thread_count < 1) {
av_log(avctx, AV_LOG_ERROR,
"automatic thread number detection not supported by codec, "
"patch welcome\n");
return -1;
}
if (s->avctx->slices > 1 || s->avctx->thread_count > 1)
s->rtp_mode = 1;
if (s->avctx->thread_count > 1 && s->codec_id == AV_CODEC_ID_H263P)
s->h263_slice_structured = 1;
if (!avctx->time_base.den || !avctx->time_base.num) {
av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
return -1;
}
i = (INT_MAX / 2 + 128) >> 8;
if (avctx->mb_threshold >= i) {
av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
i - 1);
return -1;
}
if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
av_log(avctx, AV_LOG_INFO,
"notice: b_frame_strategy only affects the first pass\n");
avctx->b_frame_strategy = 0;
}
i = av_gcd(avctx->time_base.den, avctx->time_base.num);
if (i > 1) {
av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
avctx->time_base.den /= i;
avctx->time_base.num /= i;
//return -1;
}
if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG || s->codec_id==AV_CODEC_ID_AMV) {
// (a + x * 3 / 8) / x
s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
s->inter_quant_bias = 0;
} else {
s->intra_quant_bias = 0;
// (a - x / 4) / x
s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
}
if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
s->intra_quant_bias = avctx->intra_quant_bias;
if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
s->inter_quant_bias = avctx->inter_quant_bias;
av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
s->avctx->time_base.den > (1 << 16) - 1) {
av_log(avctx, AV_LOG_ERROR,
"timebase %d/%d not supported by MPEG 4 standard, "
"the maximum admitted value for the timebase denominator "
"is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
(1 << 16) - 1);
return -1;
}
s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
switch (avctx->codec->id) {
case AV_CODEC_ID_MPEG1VIDEO:
s->out_format = FMT_MPEG1;
s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
break;
case AV_CODEC_ID_MPEG2VIDEO:
s->out_format = FMT_MPEG1;
s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
s->rtp_mode = 1;
break;
case AV_CODEC_ID_LJPEG:
case AV_CODEC_ID_MJPEG:
case AV_CODEC_ID_AMV:
s->out_format = FMT_MJPEG;
s->intra_only = 1; /* force intra only for jpeg */
if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
(avctx->pix_fmt == AV_PIX_FMT_BGR0
|| s->avctx->pix_fmt == AV_PIX_FMT_BGRA
|| s->avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
s->mjpeg_vsample[0] = s->mjpeg_vsample[1] = s->mjpeg_vsample[2] = 2;
s->mjpeg_hsample[0] = s->mjpeg_hsample[1] = s->mjpeg_hsample[2] = 1;
} else {
s->mjpeg_vsample[0] = 2;
s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
s->mjpeg_hsample[0] = 2;
s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
}
if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
ff_mjpeg_encode_init(s) < 0)
return -1;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_H261:
if (!CONFIG_H261_ENCODER)
return -1;
if (ff_h261_get_picture_format(s->width, s->height) < 0) {
av_log(avctx, AV_LOG_ERROR,
"The specified picture size of %dx%d is not valid for the "
"H.261 codec.\nValid sizes are 176x144, 352x288\n",
s->width, s->height);
return -1;
}
s->out_format = FMT_H261;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_H263:
if (!CONFIG_H263_ENCODER)
return -1;
if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
s->width, s->height) == 8) {
av_log(avctx, AV_LOG_ERROR,
"The specified picture size of %dx%d is not valid for "
"the H.263 codec.\nValid sizes are 128x96, 176x144, "
"352x288, 704x576, and 1408x1152. "
"Try H.263+.\n", s->width, s->height);
return -1;
}
s->out_format = FMT_H263;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_H263P:
s->out_format = FMT_H263;
s->h263_plus = 1;
/* Fx */
s->h263_aic = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
s->modified_quant = s->h263_aic;
s->loop_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
/* /Fx */
/* These are just to be sure */
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_FLV1:
s->out_format = FMT_H263;
s->h263_flv = 2; /* format = 1; 11-bit codes */
s->unrestricted_mv = 1;
s->rtp_mode = 0; /* don't allow GOB */
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_RV10:
s->out_format = FMT_H263;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_RV20:
s->out_format = FMT_H263;
avctx->delay = 0;
s->low_delay = 1;
s->modified_quant = 1;
s->h263_aic = 1;
s->h263_plus = 1;
s->loop_filter = 1;
s->unrestricted_mv = 0;
break;
case AV_CODEC_ID_MPEG4:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->low_delay = s->max_b_frames ? 0 : 1;
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
break;
case AV_CODEC_ID_MSMPEG4V2:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 2;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_MSMPEG4V3:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 3;
s->flipflop_rounding = 1;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_WMV1:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 4;
s->flipflop_rounding = 1;
avctx->delay = 0;
s->low_delay = 1;
break;
case AV_CODEC_ID_WMV2:
s->out_format = FMT_H263;
s->h263_pred = 1;
s->unrestricted_mv = 1;
s->msmpeg4_version = 5;
s->flipflop_rounding = 1;
avctx->delay = 0;
s->low_delay = 1;
break;
default:
return -1;
}
avctx->has_b_frames = !s->low_delay;
s->encoding = 1;
s->progressive_frame =
s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
CODEC_FLAG_INTERLACED_ME) ||
s->alternate_scan);
/* init */
if (ff_MPV_common_init(s) < 0)
return -1;
ff_dct_encode_init(s);
if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
s->chroma_qscale_table = ff_h263_chroma_qscale_table;
s->quant_precision = 5;
ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
ff_h261_encode_init(s);
if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
ff_h263_encode_init(s);
if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
ff_msmpeg4_encode_init(s);
if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
&& s->out_format == FMT_MPEG1)
ff_mpeg1_encode_init(s);
/* init q matrix */
for (i = 0; i < 64; i++) {
int j = s->dsp.idct_permutation[i];
if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
s->mpeg_quant) {
s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
} else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
s->intra_matrix[j] =
s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
} else {
/* mpeg1/2 */
s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
}
if (s->avctx->intra_matrix)
s->intra_matrix[j] = s->avctx->intra_matrix[i];
if (s->avctx->inter_matrix)
s->inter_matrix[j] = s->avctx->inter_matrix[i];
}
/* precompute matrix */
/* for mjpeg, we do include qscale in the matrix */
if (s->out_format != FMT_MJPEG) {
ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
s->intra_matrix, s->intra_quant_bias, avctx->qmin,
31, 1);
ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
s->inter_matrix, s->inter_quant_bias, avctx->qmin,
31, 0);
}
if (ff_rate_control_init(s) < 0)
return -1;
return 0;
}
av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
ff_rate_control_uninit(s);
ff_MPV_common_end(s);
if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
s->out_format == FMT_MJPEG)
ff_mjpeg_encode_close(s);
av_freep(&avctx->extradata);
return 0;
}
static int get_sae(uint8_t *src, int ref, int stride)
{
int x,y;
int acc = 0;
for (y = 0; y < 16; y++) {
for (x = 0; x < 16; x++) {
acc += FFABS(src[x + y * stride] - ref);
}
}
return acc;
}
static int get_intra_count(MpegEncContext *s, uint8_t *src,
uint8_t *ref, int stride)
{
int x, y, w, h;
int acc = 0;
w = s->width & ~15;
h = s->height & ~15;
for (y = 0; y < h; y += 16) {
for (x = 0; x < w; x += 16) {
int offset = x + y * stride;
int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
16);
int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
int sae = get_sae(src + offset, mean, stride);
acc += sae + 500 < sad;
}
}
return acc;
}
static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
{
Picture *pic = NULL;
int64_t pts;
int i, display_picture_number = 0, ret;
const int encoding_delay = s->max_b_frames ? s->max_b_frames :
(s->low_delay ? 0 : 1);
int direct = 1;
if (pic_arg) {
pts = pic_arg->pts;
display_picture_number = s->input_picture_number++;
if (pts != AV_NOPTS_VALUE) {
if (s->user_specified_pts != AV_NOPTS_VALUE) {
int64_t time = pts;
int64_t last = s->user_specified_pts;
if (time <= last) {
av_log(s->avctx, AV_LOG_ERROR,
"Error, Invalid timestamp=%"PRId64", "
"last=%"PRId64"\n", pts, s->user_specified_pts);
return -1;
}
if (!s->low_delay && display_picture_number == 1)
s->dts_delta = time - last;
}
s->user_specified_pts = pts;
} else {
if (s->user_specified_pts != AV_NOPTS_VALUE) {
s->user_specified_pts =
pts = s->user_specified_pts + 1;
av_log(s->avctx, AV_LOG_INFO,
"Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
pts);
} else {
pts = display_picture_number;
}
}
}
if (pic_arg) {
if (!pic_arg->buf[0])
direct = 0;
if (pic_arg->linesize[0] != s->linesize)
direct = 0;
if (pic_arg->linesize[1] != s->uvlinesize)
direct = 0;
if (pic_arg->linesize[2] != s->uvlinesize)
direct = 0;
av_dlog(s->avctx, "%d %d %d %d\n", pic_arg->linesize[0],
pic_arg->linesize[1], s->linesize, s->uvlinesize);
if (direct) {
i = ff_find_unused_picture(s, 1);