-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotion.c
1647 lines (1477 loc) · 49.9 KB
/
motion.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
/* motion.c, motion estimation */
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
#include <stdio.h>
#include "global.h"
/* private prototypes */
static void frame_ME(simpeg_encode_context * context,
unsigned char *oldorg, unsigned char *neworg,
unsigned char *oldref, unsigned char *newref, unsigned char *cur,
int i, int j, int sxf, int syf, int sxb, int syb, struct mbinfo *mbi);
static void field_ME(simpeg_encode_context * context,
unsigned char *oldorg, unsigned char *neworg,
unsigned char *oldref, unsigned char *newref, unsigned char *cur,
unsigned char *curref, int i, int j, int sxf, int syf, int sxb, int syb,
struct mbinfo *mbi, int secondfield, int ipflag);
static void frame_estimate(simpeg_encode_context * context,
unsigned char *org,
unsigned char *ref, unsigned char *mb,
int i, int j,
int sx, int sy, int *iminp, int *jminp, int *imintp, int *jmintp,
int *iminbp, int *jminbp, int *dframep, int *dfieldp,
int *tselp, int *bselp, int imins[2][2], int jmins[2][2]);
static void field_estimate(simpeg_encode_context * context,
unsigned char *toporg,
unsigned char *topref, unsigned char *botorg, unsigned char *botref,
unsigned char *mb, int i, int j, int sx, int sy, int ipflag,
int *iminp, int *jminp, int *imin8up, int *jmin8up, int *imin8lp,
int *jmin8lp, int *dfieldp, int *d8p, int *selp, int *sel8up, int *sel8lp,
int *iminsp, int *jminsp, int *dsp);
static void dpframe_estimate(simpeg_encode_context * context,
unsigned char *ref,
unsigned char *mb, int i, int j, int iminf[2][2], int jminf[2][2],
int *iminp, int *jminp, int *imindmvp, int *jmindmvp,
int *dmcp, int *vmcp);
static void dpfield_estimate(simpeg_encode_context * context,
unsigned char *topref,
unsigned char *botref, unsigned char *mb,
int i, int j, int imins, int jmins, int *imindmvp, int *jmindmvp,
int *dmcp, int *vmcp);
static int fullsearch(unsigned char *org, unsigned char *ref,
unsigned char *blk,
int lx, int i0, int j0, int sx, int sy, int h, int xmax, int ymax,
int *iminp, int *jminp);
static int dist1(unsigned char *blk1, unsigned char *blk2,
int lx, int hx, int hy, int h, int distlim);
static int dist2(unsigned char *blk1, unsigned char *blk2,
int lx, int hx, int hy, int h);
static int bdist1(unsigned char *pf, unsigned char *pb,
unsigned char *p2, int lx, int hxf, int hyf, int hxb, int hyb, int h);
static int bdist2(unsigned char *pf, unsigned char *pb,
unsigned char *p2, int lx, int hxf, int hyf, int hxb, int hyb, int h);
static int variance(unsigned char *p, int lx);
/*
* motion estimation for progressive and interlaced frame pictures
*
* oldorg: source frame for forward prediction (used for P and B frames)
* neworg: source frame for backward prediction (B frames only)
* oldref: reconstructed frame for forward prediction (P and B frames)
* newref: reconstructed frame for backward prediction (B frames only)
* cur: current frame (the one for which the prediction is formed)
* sxf,syf: forward search window (frame coordinates)
* sxb,syb: backward search window (frame coordinates)
* mbi: pointer to macroblock info structure
*
* results:
* mbi->
* mb_type: 0, MB_INTRA, MB_FORWARD, MB_BACKWARD, MB_FORWARD|MB_BACKWARD
* MV[][][]: motion vectors (frame format)
* mv_field_sel: top/bottom field (for field prediction)
* motion_type: MC_FRAME, MC_FIELD
*
* uses global vars: pict_type, frame_pred_dct
*/
void
simpeg_encode_motion_estimation(simpeg_encode_context * context,
unsigned char *oldorg, unsigned char *neworg, unsigned char *oldref,
unsigned char *newref, unsigned char *cur, unsigned char *curref,
int sxf, int syf, int sxb, int syb, struct mbinfo *mbi, int secondfield,
int ipflag)
{
int i, j;
/* loop through all macroblocks of the picture */
for (j=0; j<context->height2; j+=16)
{
for (i=0; i<context->width; i+=16)
{
if (context->pict_struct==FRAME_PICTURE)
frame_ME(context,oldorg,neworg,oldref,newref,cur,i,j,sxf,syf,sxb,syb,mbi);
else
field_ME(context,oldorg,neworg,oldref,newref,cur,curref,i,j,sxf,syf,sxb,syb,
mbi,secondfield,ipflag);
mbi++;
}
if (!context->quiet)
{
SimpegWrite_progress(context,(float)j/(float)context->height2,
context->SimpegWrite_current_frame, context->nframes);
}
}
if (!context->quiet)
{
SimpegWrite_progress(context,1.0, context->SimpegWrite_current_frame,
context->nframes);
}
}
static void frame_ME(simpeg_encode_context * context,
unsigned char *oldorg, unsigned char *neworg, unsigned char *oldref,
unsigned char *newref, unsigned char *cur, int i, int j, int sxf,
int syf, int sxb, int syb, struct mbinfo *mbi)
{
int imin,jmin,iminf,jminf,iminr,jminr;
int imint,jmint,iminb,jminb;
int imintf,jmintf,iminbf,jminbf;
int imintr,jmintr,iminbr,jminbr;
int var,v0;
int dmc,dmcf,dmcr,dmci,vmc,vmcf,vmcr,vmci;
int dmcfield,dmcfieldf,dmcfieldr,dmcfieldi;
int tsel,bsel,tself,bself,tselr,bselr;
unsigned char *mb;
int imins[2][2],jmins[2][2];
int imindp,jmindp,imindmv,jmindmv,dmc_dp,vmc_dp;
int width, height;
height = context->height;
width = context->width;
mb = cur + i + width*j;
var = variance(mb,width);
if (context->pict_type==I_TYPE)
mbi->mb_type = MB_INTRA;
else if (context->pict_type==P_TYPE)
{
if (context->frame_pred_dct)
{
dmc = fullsearch(oldorg,oldref,mb,
width,i,j,sxf,syf,16,width,height,&imin,&jmin);
vmc = dist2(oldref+(imin>>1)+width*(jmin>>1),mb,
width,imin&1,jmin&1,16);
mbi->motion_type = MC_FRAME;
}
else
{
frame_estimate(context,oldorg,oldref,mb,i,j,sxf,syf,
&imin,&jmin,&imint,&jmint,&iminb,&jminb,
&dmc,&dmcfield,&tsel,&bsel,imins,jmins);
if (context->M==1)
dpframe_estimate(context,oldref,mb,i,j>>1,imins,jmins,
&imindp,&jmindp,&imindmv,&jmindmv,&dmc_dp,&vmc_dp);
/* select between dual prime, frame and field prediction */
if (context->M==1 && dmc_dp<dmc && dmc_dp<dmcfield)
{
mbi->motion_type = MC_DMV;
dmc = dmc_dp;
vmc = vmc_dp;
}
else if (dmc<=dmcfield)
{
mbi->motion_type = MC_FRAME;
vmc = dist2(oldref+(imin>>1)+width*(jmin>>1),mb,
width,imin&1,jmin&1,16);
}
else
{
mbi->motion_type = MC_FIELD;
dmc = dmcfield;
vmc = dist2(oldref+(tsel?width:0)+(imint>>1)+(width<<1)*(jmint>>1),
mb,width<<1,imint&1,jmint&1,8);
vmc+= dist2(oldref+(bsel?width:0)+(iminb>>1)+(width<<1)*(jminb>>1),
mb+width,width<<1,iminb&1,jminb&1,8);
}
}
/* select between intra or non-intra coding:
*
* selection is based on intra block variance (var) vs.
* prediction error variance (vmc)
*
* blocks with small prediction error are always coded non-intra
* even if variance is smaller (is this reasonable?)
*/
if (vmc>var && vmc>=9*256)
mbi->mb_type = MB_INTRA;
else
{
/* select between MC / No-MC
*
* use No-MC if var(No-MC) <= 1.25*var(MC)
* (i.e slightly biased towards No-MC)
*
* blocks with small prediction error are always coded as No-MC
* (requires no motion vectors, allows skipping)
*/
v0 = dist2(oldref+i+width*j,mb,width,0,0,16);
if (4*v0>5*vmc && v0>=9*256)
{
/* use MC */
var = vmc;
mbi->mb_type = MB_FORWARD;
if (mbi->motion_type==MC_FRAME)
{
mbi->MV[0][0][0] = imin - (i<<1);
mbi->MV[0][0][1] = jmin - (j<<1);
}
else if (mbi->motion_type==MC_DMV)
{
/* these are FRAME vectors */
/* same parity vector */
mbi->MV[0][0][0] = imindp - (i<<1);
mbi->MV[0][0][1] = (jmindp<<1) - (j<<1);
/* opposite parity vector */
mbi->dmvector[0] = imindmv;
mbi->dmvector[1] = jmindmv;
}
else
{
/* these are FRAME vectors */
mbi->MV[0][0][0] = imint - (i<<1);
mbi->MV[0][0][1] = (jmint<<1) - (j<<1);
mbi->MV[1][0][0] = iminb - (i<<1);
mbi->MV[1][0][1] = (jminb<<1) - (j<<1);
mbi->mv_field_sel[0][0] = tsel;
mbi->mv_field_sel[1][0] = bsel;
}
}
else
{
/* No-MC */
var = v0;
mbi->mb_type = 0;
mbi->motion_type = MC_FRAME;
mbi->MV[0][0][0] = 0;
mbi->MV[0][0][1] = 0;
}
}
}
else /* if (pict_type==B_TYPE) */
{
if (context->frame_pred_dct)
{
/* forward */
dmcf = fullsearch(oldorg,oldref,mb,
width,i,j,sxf,syf,16,width,height,&iminf,&jminf);
vmcf = dist2(oldref+(iminf>>1)+width*(jminf>>1),mb,
width,iminf&1,jminf&1,16);
/* backward */
dmcr = fullsearch(neworg,newref,mb,
width,i,j,sxb,syb,16,width,height,&iminr,&jminr);
vmcr = dist2(newref+(iminr>>1)+width*(jminr>>1),mb,
width,iminr&1,jminr&1,16);
/* interpolated (bidirectional) */
vmci = bdist2(oldref+(iminf>>1)+width*(jminf>>1),
newref+(iminr>>1)+width*(jminr>>1),
mb,width,iminf&1,jminf&1,iminr&1,jminr&1,16);
/* decisions */
/* select between forward/backward/interpolated prediction:
* use the one with smallest mean sqaured prediction error
*/
if (vmcf<=vmcr && vmcf<=vmci)
{
vmc = vmcf;
mbi->mb_type = MB_FORWARD;
}
else if (vmcr<=vmci)
{
vmc = vmcr;
mbi->mb_type = MB_BACKWARD;
}
else
{
vmc = vmci;
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
}
mbi->motion_type = MC_FRAME;
}
else
{
/* forward prediction */
frame_estimate(context,oldorg,oldref,mb,i,j,sxf,syf,
&iminf,&jminf,&imintf,&jmintf,&iminbf,&jminbf,
&dmcf,&dmcfieldf,&tself,&bself,imins,jmins);
/* backward prediction */
frame_estimate(context,neworg,newref,mb,i,j,sxb,syb,
&iminr,&jminr,&imintr,&jmintr,&iminbr,&jminbr,
&dmcr,&dmcfieldr,&tselr,&bselr,imins,jmins);
/* calculate interpolated distance */
/* frame */
dmci = bdist1(oldref+(iminf>>1)+width*(jminf>>1),
newref+(iminr>>1)+width*(jminr>>1),
mb,width,iminf&1,jminf&1,iminr&1,jminr&1,16);
/* top field */
dmcfieldi = bdist1(
oldref+(imintf>>1)+(tself?width:0)+(width<<1)*(jmintf>>1),
newref+(imintr>>1)+(tselr?width:0)+(width<<1)*(jmintr>>1),
mb,width<<1,imintf&1,jmintf&1,imintr&1,jmintr&1,8);
/* bottom field */
dmcfieldi+= bdist1(
oldref+(iminbf>>1)+(bself?width:0)+(width<<1)*(jminbf>>1),
newref+(iminbr>>1)+(bselr?width:0)+(width<<1)*(jminbr>>1),
mb+width,width<<1,iminbf&1,jminbf&1,iminbr&1,jminbr&1,8);
/* select prediction type of minimum distance from the
* six candidates (field/frame * forward/backward/interpolated)
*/
if (dmci<dmcfieldi && dmci<dmcf && dmci<dmcfieldf
&& dmci<dmcr && dmci<dmcfieldr)
{
/* frame, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_FRAME;
vmc = bdist2(oldref+(iminf>>1)+width*(jminf>>1),
newref+(iminr>>1)+width*(jminr>>1),
mb,width,iminf&1,jminf&1,iminr&1,jminr&1,16);
}
else if (dmcfieldi<dmcf && dmcfieldi<dmcfieldf
&& dmcfieldi<dmcr && dmcfieldi<dmcfieldr)
{
/* field, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = bdist2(oldref+(imintf>>1)+(tself?width:0)+(width<<1)*(jmintf>>1),
newref+(imintr>>1)+(tselr?width:0)+(width<<1)*(jmintr>>1),
mb,width<<1,imintf&1,jmintf&1,imintr&1,jmintr&1,8);
vmc+= bdist2(oldref+(iminbf>>1)+(bself?width:0)+(width<<1)*(jminbf>>1),
newref+(iminbr>>1)+(bselr?width:0)+(width<<1)*(jminbr>>1),
mb+width,width<<1,iminbf&1,jminbf&1,iminbr&1,jminbr&1,8);
}
else if (dmcf<dmcfieldf && dmcf<dmcr && dmcf<dmcfieldr)
{
/* frame, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_FRAME;
vmc = dist2(oldref+(iminf>>1)+width*(jminf>>1),mb,
width,iminf&1,jminf&1,16);
}
else if (dmcfieldf<dmcr && dmcfieldf<dmcfieldr)
{
/* field, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(oldref+(tself?width:0)+(imintf>>1)+(width<<1)*(jmintf>>1),
mb,width<<1,imintf&1,jmintf&1,8);
vmc+= dist2(oldref+(bself?width:0)+(iminbf>>1)+(width<<1)*(jminbf>>1),
mb+width,width<<1,iminbf&1,jminbf&1,8);
}
else if (dmcr<dmcfieldr)
{
/* frame, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_FRAME;
vmc = dist2(newref+(iminr>>1)+width*(jminr>>1),mb,
width,iminr&1,jminr&1,16);
}
else
{
/* field, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(newref+(tselr?width:0)+(imintr>>1)+(width<<1)*(jmintr>>1),
mb,width<<1,imintr&1,jmintr&1,8);
vmc+= dist2(newref+(bselr?width:0)+(iminbr>>1)+(width<<1)*(jminbr>>1),
mb+width,width<<1,iminbr&1,jminbr&1,8);
}
}
/* select between intra or non-intra coding:
*
* selection is based on intra block variance (var) vs.
* prediction error variance (vmc)
*
* blocks with small prediction error are always coded non-intra
* even if variance is smaller (is this reasonable?)
*/
if (vmc>var && vmc>=9*256)
mbi->mb_type = MB_INTRA;
else
{
var = vmc;
if (mbi->motion_type==MC_FRAME)
{
/* forward */
mbi->MV[0][0][0] = iminf - (i<<1);
mbi->MV[0][0][1] = jminf - (j<<1);
/* backward */
mbi->MV[0][1][0] = iminr - (i<<1);
mbi->MV[0][1][1] = jminr - (j<<1);
}
else
{
/* these are FRAME vectors */
/* forward */
mbi->MV[0][0][0] = imintf - (i<<1);
mbi->MV[0][0][1] = (jmintf<<1) - (j<<1);
mbi->MV[1][0][0] = iminbf - (i<<1);
mbi->MV[1][0][1] = (jminbf<<1) - (j<<1);
mbi->mv_field_sel[0][0] = tself;
mbi->mv_field_sel[1][0] = bself;
/* backward */
mbi->MV[0][1][0] = imintr - (i<<1);
mbi->MV[0][1][1] = (jmintr<<1) - (j<<1);
mbi->MV[1][1][0] = iminbr - (i<<1);
mbi->MV[1][1][1] = (jminbr<<1) - (j<<1);
mbi->mv_field_sel[0][1] = tselr;
mbi->mv_field_sel[1][1] = bselr;
}
}
}
mbi->var = var;
}
/*
* motion estimation for field pictures
*
* oldorg: original frame for forward prediction (P and B frames)
* neworg: original frame for backward prediction (B frames only)
* oldref: reconstructed frame for forward prediction (P and B frames)
* newref: reconstructed frame for backward prediction (B frames only)
* cur: current original frame (the one for which the prediction is formed)
* curref: current reconstructed frame (to predict second field from first)
* sxf,syf: forward search window (frame coordinates)
* sxb,syb: backward search window (frame coordinates)
* mbi: pointer to macroblock info structure
* secondfield: indicates second field of a frame (in P fields this means
* that reference field of opposite parity is in curref instead
* of oldref)
* ipflag: indicates a P type field which is the second field of a frame
* in which the first field is I type (this restricts predictions
* to be based only on the opposite parity (=I) field)
*
* results:
* mbi->
* mb_type: 0, MB_INTRA, MB_FORWARD, MB_BACKWARD, MB_FORWARD|MB_BACKWARD
* MV[][][]: motion vectors (field format)
* mv_field_sel: top/bottom field
* motion_type: MC_FIELD, MC_16X8
*
* uses global vars: pict_type, pict_struct
*/
static void field_ME(simpeg_encode_context * context,
unsigned char *oldorg, unsigned char *neworg,
unsigned char *oldref, unsigned char *newref,
unsigned char *cur, unsigned char *curref, int i, int j,
int sxf, int syf, int sxb, int syb, struct mbinfo *mbi, int secondfield, int ipflag)
{
int w2;
unsigned char *mb, *toporg, *topref, *botorg, *botref;
int var,vmc,v0,dmc,dmcfieldi,dmc8i;
int imin,jmin,imin8u,jmin8u,imin8l,jmin8l,dmcfield,dmc8,sel,sel8u,sel8l;
int iminf,jminf,imin8uf,jmin8uf,imin8lf,jmin8lf,dmcfieldf,dmc8f,self,sel8uf,sel8lf;
int iminr,jminr,imin8ur,jmin8ur,imin8lr,jmin8lr,dmcfieldr,dmc8r,selr,sel8ur,sel8lr;
int imins,jmins,ds,imindmv,jmindmv,vmc_dp,dmc_dp;
int width;
width = context->width;
w2 = width<<1;
mb = cur + i + w2*j;
if (context->pict_struct==BOTTOM_FIELD)
mb += width;
var = variance(mb,w2);
if (context->pict_type==I_TYPE)
mbi->mb_type = MB_INTRA;
else if (context->pict_type==P_TYPE)
{
toporg = oldorg;
topref = oldref;
botorg = oldorg + width;
botref = oldref + width;
if (secondfield)
{
/* opposite parity field is in same frame */
if (context->pict_struct==TOP_FIELD)
{
/* current is top field */
botorg = cur + width;
botref = curref + width;
}
else
{
/* current is bottom field */
toporg = cur;
topref = curref;
}
}
field_estimate(context,toporg,topref,botorg,botref,mb,i,j,sxf,syf,ipflag,
&imin,&jmin,&imin8u,&jmin8u,&imin8l,&jmin8l,
&dmcfield,&dmc8,&sel,&sel8u,&sel8l,&imins,&jmins,&ds);
if (context->M==1 && !ipflag) /* generic condition which permits Dual Prime */
dpfield_estimate(context,topref,botref,mb,i,j,imins,jmins,&imindmv,&jmindmv,
&dmc_dp,&vmc_dp);
/* select between dual prime, field and 16x8 prediction */
if (context->M==1 && !ipflag && dmc_dp<dmc8 && dmc_dp<dmcfield)
{
/* Dual Prime prediction */
mbi->motion_type = MC_DMV;
dmc = dmc_dp; /* L1 metric */
vmc = vmc_dp; /* we already calculated L2 error for Dual */
}
else if (dmc8<dmcfield)
{
/* 16x8 prediction */
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = dist2((sel8u?botref:topref) + (imin8u>>1) + w2*(jmin8u>>1),
mb,w2,imin8u&1,jmin8u&1,8);
/* lower half block */
vmc+= dist2((sel8l?botref:topref) + (imin8l>>1) + w2*(jmin8l>>1),
mb+8*w2,w2,imin8l&1,jmin8l&1,8);
}
else
{
/* field prediction */
mbi->motion_type = MC_FIELD;
vmc = dist2((sel?botref:topref) + (imin>>1) + w2*(jmin>>1),
mb,w2,imin&1,jmin&1,16);
}
/* select between intra and non-intra coding */
if (vmc>var && vmc>=9*256)
mbi->mb_type = MB_INTRA;
else
{
/* zero MV field prediction from same parity ref. field
* (not allowed if ipflag is set)
*/
if (!ipflag)
v0 = dist2(((context->pict_struct==BOTTOM_FIELD)?botref:topref) + i + w2*j,
mb,w2,0,0,16);
if (ipflag || (4*v0>5*vmc && v0>=9*256))
{
var = vmc;
mbi->mb_type = MB_FORWARD;
if (mbi->motion_type==MC_FIELD)
{
mbi->MV[0][0][0] = imin - (i<<1);
mbi->MV[0][0][1] = jmin - (j<<1);
mbi->mv_field_sel[0][0] = sel;
}
else if (mbi->motion_type==MC_DMV)
{
/* same parity vector */
mbi->MV[0][0][0] = imins - (i<<1);
mbi->MV[0][0][1] = jmins - (j<<1);
/* opposite parity vector */
mbi->dmvector[0] = imindmv;
mbi->dmvector[1] = jmindmv;
}
else
{
mbi->MV[0][0][0] = imin8u - (i<<1);
mbi->MV[0][0][1] = jmin8u - (j<<1);
mbi->MV[1][0][0] = imin8l - (i<<1);
mbi->MV[1][0][1] = jmin8l - ((j+8)<<1);
mbi->mv_field_sel[0][0] = sel8u;
mbi->mv_field_sel[1][0] = sel8l;
}
}
else
{
/* No MC */
var = v0;
mbi->mb_type = 0;
mbi->motion_type = MC_FIELD;
mbi->MV[0][0][0] = 0;
mbi->MV[0][0][1] = 0;
mbi->mv_field_sel[0][0] = (context->pict_struct==BOTTOM_FIELD);
}
}
}
else /* if (pict_type==B_TYPE) */
{
/* forward prediction */
field_estimate(context,oldorg,oldref,oldorg+width,oldref+width,mb,
i,j,sxf,syf,0,
&iminf,&jminf,&imin8uf,&jmin8uf,&imin8lf,&jmin8lf,
&dmcfieldf,&dmc8f,&self,&sel8uf,&sel8lf,&imins,&jmins,&ds);
/* backward prediction */
field_estimate(context,neworg,newref,neworg+width,newref+width,mb,
i,j,sxb,syb,0,
&iminr,&jminr,&imin8ur,&jmin8ur,&imin8lr,&jmin8lr,
&dmcfieldr,&dmc8r,&selr,&sel8ur,&sel8lr,&imins,&jmins,&ds);
/* calculate distances for bidirectional prediction */
/* field */
dmcfieldi = bdist1(oldref + (self?width:0) + (iminf>>1) + w2*(jminf>>1),
newref + (selr?width:0) + (iminr>>1) + w2*(jminr>>1),
mb,w2,iminf&1,jminf&1,iminr&1,jminr&1,16);
/* 16x8 upper half block */
dmc8i = bdist1(oldref + (sel8uf?width:0) + (imin8uf>>1) + w2*(jmin8uf>>1),
newref + (sel8ur?width:0) + (imin8ur>>1) + w2*(jmin8ur>>1),
mb,w2,imin8uf&1,jmin8uf&1,imin8ur&1,jmin8ur&1,8);
/* 16x8 lower half block */
dmc8i+= bdist1(oldref + (sel8lf?width:0) + (imin8lf>>1) + w2*(jmin8lf>>1),
newref + (sel8lr?width:0) + (imin8lr>>1) + w2*(jmin8lr>>1),
mb+8*w2,w2,imin8lf&1,jmin8lf&1,imin8lr&1,jmin8lr&1,8);
/* select prediction type of minimum distance */
if (dmcfieldi<dmc8i && dmcfieldi<dmcfieldf && dmcfieldi<dmc8f
&& dmcfieldi<dmcfieldr && dmcfieldi<dmc8r)
{
/* field, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = bdist2(oldref + (self?width:0) + (iminf>>1) + w2*(jminf>>1),
newref + (selr?width:0) + (iminr>>1) + w2*(jminr>>1),
mb,w2,iminf&1,jminf&1,iminr&1,jminr&1,16);
}
else if (dmc8i<dmcfieldf && dmc8i<dmc8f
&& dmc8i<dmcfieldr && dmc8i<dmc8r)
{
/* 16x8, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = bdist2(oldref + (sel8uf?width:0) + (imin8uf>>1) + w2*(jmin8uf>>1),
newref + (sel8ur?width:0) + (imin8ur>>1) + w2*(jmin8ur>>1),
mb,w2,imin8uf&1,jmin8uf&1,imin8ur&1,jmin8ur&1,8);
/* lower half block */
vmc+= bdist2(oldref + (sel8lf?width:0) + (imin8lf>>1) + w2*(jmin8lf>>1),
newref + (sel8lr?width:0) + (imin8lr>>1) + w2*(jmin8lr>>1),
mb+8*w2,w2,imin8lf&1,jmin8lf&1,imin8lr&1,jmin8lr&1,8);
}
else if (dmcfieldf<dmc8f && dmcfieldf<dmcfieldr && dmcfieldf<dmc8r)
{
/* field, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(oldref + (self?width:0) + (iminf>>1) + w2*(jminf>>1),
mb,w2,iminf&1,jminf&1,16);
}
else if (dmc8f<dmcfieldr && dmc8f<dmc8r)
{
/* 16x8, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = dist2(oldref + (sel8uf?width:0) + (imin8uf>>1) + w2*(jmin8uf>>1),
mb,w2,imin8uf&1,jmin8uf&1,8);
/* lower half block */
vmc+= dist2(oldref + (sel8lf?width:0) + (imin8lf>>1) + w2*(jmin8lf>>1),
mb+8*w2,w2,imin8lf&1,jmin8lf&1,8);
}
else if (dmcfieldr<dmc8r)
{
/* field, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(newref + (selr?width:0) + (iminr>>1) + w2*(jminr>>1),
mb,w2,iminr&1,jminr&1,16);
}
else
{
/* 16x8, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = dist2(newref + (sel8ur?width:0) + (imin8ur>>1) + w2*(jmin8ur>>1),
mb,w2,imin8ur&1,jmin8ur&1,8);
/* lower half block */
vmc+= dist2(newref + (sel8lr?width:0) + (imin8lr>>1) + w2*(jmin8lr>>1),
mb+8*w2,w2,imin8lr&1,jmin8lr&1,8);
}
/* select between intra and non-intra coding */
if (vmc>var && vmc>=9*256)
mbi->mb_type = MB_INTRA;
else
{
var = vmc;
if (mbi->motion_type==MC_FIELD)
{
/* forward */
mbi->MV[0][0][0] = iminf - (i<<1);
mbi->MV[0][0][1] = jminf - (j<<1);
mbi->mv_field_sel[0][0] = self;
/* backward */
mbi->MV[0][1][0] = iminr - (i<<1);
mbi->MV[0][1][1] = jminr - (j<<1);
mbi->mv_field_sel[0][1] = selr;
}
else /* MC_16X8 */
{
/* forward */
mbi->MV[0][0][0] = imin8uf - (i<<1);
mbi->MV[0][0][1] = jmin8uf - (j<<1);
mbi->mv_field_sel[0][0] = sel8uf;
mbi->MV[1][0][0] = imin8lf - (i<<1);
mbi->MV[1][0][1] = jmin8lf - ((j+8)<<1);
mbi->mv_field_sel[1][0] = sel8lf;
/* backward */
mbi->MV[0][1][0] = imin8ur - (i<<1);
mbi->MV[0][1][1] = jmin8ur - (j<<1);
mbi->mv_field_sel[0][1] = sel8ur;
mbi->MV[1][1][0] = imin8lr - (i<<1);
mbi->MV[1][1][1] = jmin8lr - ((j+8)<<1);
mbi->mv_field_sel[1][1] = sel8lr;
}
}
}
mbi->var = var;
}
/*
* frame picture motion estimation
*
* org: top left pel of source reference frame
* ref: top left pel of reconstructed reference frame
* mb: macroblock to be matched
* i,j: location of mb relative to ref (=center of search window)
* sx,sy: half widths of search window
* iminp,jminp,dframep: location and value of best frame prediction
* imintp,jmintp,tselp: location of best field pred. for top field of mb
* iminbp,jminbp,bselp: location of best field pred. for bottom field of mb
* dfieldp: value of field prediction
*/
static void frame_estimate(simpeg_encode_context * context,
unsigned char *org, unsigned char *ref, unsigned char *mb,
int i, int j, int sx, int sy, int *iminp, int *jminp, int *imintp,
int *jmintp, int *iminbp, int *jminbp, int *dframep, int *dfieldp,
int *tselp, int *bselp, int (*imins)[2], int (*jmins)[2])
{
int dt,db,dmint,dminb;
int imint,iminb,jmint,jminb;
int width, height;
width = context->width;
height = context->height;
/* frame prediction */
*dframep = fullsearch(org,ref,mb,width,i,j,sx,sy,16,width,height,
iminp,jminp);
/* predict top field from top field */
dt = fullsearch(org,ref,mb,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict top field from bottom field */
db = fullsearch(org+width,ref+width,mb,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
&iminb,&jminb);
imins[0][0] = imint;
jmins[0][0] = jmint;
imins[1][0] = iminb;
jmins[1][0] = jminb;
/* select prediction for top field */
if (dt<=db)
{
dmint=dt; *imintp=imint; *jmintp=jmint; *tselp=0;
}
else
{
dmint=db; *imintp=iminb; *jmintp=jminb; *tselp=1;
}
/* predict bottom field from top field */
dt = fullsearch(org,ref,mb+width,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict bottom field from bottom field */
db = fullsearch(org+width,ref+width,mb+width,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
&iminb,&jminb);
imins[0][1] = imint;
jmins[0][1] = jmint;
imins[1][1] = iminb;
jmins[1][1] = jminb;
/* select prediction for bottom field */
if (db<=dt)
{
dminb=db; *iminbp=iminb; *jminbp=jminb; *bselp=1;
}
else
{
dminb=dt; *iminbp=imint; *jminbp=jmint; *bselp=0;
}
*dfieldp=dmint+dminb;
}
/*
* field picture motion estimation subroutine
*
* toporg: address of original top reference field
* topref: address of reconstructed top reference field
* botorg: address of original bottom reference field
* botref: address of reconstructed bottom reference field
* mb: macroblock to be matched
* i,j: location of mb (=center of search window)
* sx,sy: half width/height of search window
*
* iminp,jminp,selp,dfieldp: location and distance of best field prediction
* imin8up,jmin8up,sel8up: location of best 16x8 pred. for upper half of mb
* imin8lp,jmin8lp,sel8lp: location of best 16x8 pred. for lower half of mb
* d8p: distance of best 16x8 prediction
* iminsp,jminsp,dsp: location and distance of best same parity field
* prediction (needed for dual prime, only valid if
* ipflag==0)
*/
static void field_estimate(simpeg_encode_context * context,
unsigned char *toporg, unsigned char *topref, unsigned char *botorg,
unsigned char *botref, unsigned char *mb, int i, int j, int sx, int sy,
int ipflag, int *iminp, int *jminp, int *imin8up, int *jmin8up,
int *imin8lp, int *jmin8lp, int *dfieldp, int *d8p, int *selp,
int *sel8up, int *sel8lp, int *iminsp, int *jminsp, int *dsp)
{
int dt, db, imint, jmint, iminb, jminb, notop, nobot;
int width, height;
width = context->width;
height = context->height;
/* if ipflag is set, predict from field of opposite parity only */
notop = ipflag && (context->pict_struct==TOP_FIELD);
nobot = ipflag && (context->pict_struct==BOTTOM_FIELD);
/* field prediction */
/* predict current field from top field */
if (notop)
dt = 65536; /* infinity */
else
dt = fullsearch(toporg,topref,mb,width<<1,
i,j,sx,sy>>1,16,width,height>>1,
&imint,&jmint);
/* predict current field from bottom field */
if (nobot)
db = 65536; /* infinity */
else
db = fullsearch(botorg,botref,mb,width<<1,
i,j,sx,sy>>1,16,width,height>>1,
&iminb,&jminb);
/* same parity prediction (only valid if ipflag==0) */
if (context->pict_struct==TOP_FIELD)
{
*iminsp = imint; *jminsp = jmint; *dsp = dt;
}
else
{
*iminsp = iminb; *jminsp = jminb; *dsp = db;
}
/* select field prediction */
if (dt<=db)
{
*dfieldp = dt; *iminp = imint; *jminp = jmint; *selp = 0;
}
else
{
*dfieldp = db; *iminp = iminb; *jminp = jminb; *selp = 1;
}
/* 16x8 motion compensation */
/* predict upper half field from top field */
if (notop)
dt = 65536;
else
dt = fullsearch(toporg,topref,mb,width<<1,
i,j,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict upper half field from bottom field */
if (nobot)
db = 65536;
else
db = fullsearch(botorg,botref,mb,width<<1,
i,j,sx,sy>>1,8,width,height>>1,
&iminb,&jminb);
/* select prediction for upper half field */
if (dt<=db)
{
*d8p = dt; *imin8up = imint; *jmin8up = jmint; *sel8up = 0;
}
else
{
*d8p = db; *imin8up = iminb; *jmin8up = jminb; *sel8up = 1;
}
/* predict lower half field from top field */
if (notop)
dt = 65536;
else
dt = fullsearch(toporg,topref,mb+(width<<4),width<<1,
i,j+8,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict lower half field from bottom field */
if (nobot)
db = 65536;
else
db = fullsearch(botorg,botref,mb+(width<<4),width<<1,
i,j+8,sx,sy>>1,8,width,height>>1,
&iminb,&jminb);
/* select prediction for lower half field */
if (dt<=db)
{
*d8p += dt; *imin8lp = imint; *jmin8lp = jmint; *sel8lp = 0;
}
else
{
*d8p += db; *imin8lp = iminb; *jmin8lp = jminb; *sel8lp = 1;
}
}
static void dpframe_estimate(simpeg_encode_context * context,
unsigned char *ref, unsigned char *mb, int i, int j,
int (*iminf)[2], int (*jminf)[2], int *iminp, int *jminp,
int *imindmvp, int *jmindmvp, int *dmcp, int *vmcp)
{
int pref,ppred,delta_x,delta_y;
int is,js,it,jt,ib,jb,it0,jt0,ib0,jb0;
int imins,jmins,imint,jmint,iminb,jminb,imindmv,jmindmv;
int vmc,local_dist;
int width, height;