annotate mpeg12enc.c @ 7069:92bcfa9ba977 libavcodec

Factorize I >> (c->tbls->bits - 1) out.
author michael
date Thu, 19 Jun 2008 10:29:36 +0000
parents e943e1409077
children 8e25ae9f1c4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
1 /*
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
2 * MPEG1/2 encoder
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
3 * Copyright (c) 2000,2001 Fabrice Bellard.
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
5 *
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
6 * This file is part of FFmpeg.
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
7 *
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
12 *
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
16 * Lesser General Public License for more details.
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
17 *
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
21 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
22
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
23 /**
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
24 * @file mpeg12enc.c
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
25 * MPEG1/2 encoder
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
26 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
27
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
28 #include "avcodec.h"
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
29 #include "dsputil.h"
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
30 #include "mpegvideo.h"
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
31
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
32 #include "mpeg12.h"
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
33 #include "mpeg12data.h"
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
34 #include "bytestream.h"
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
35
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
36
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
37 static const uint8_t inv_non_linear_qscale[13] = {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
38 0, 2, 4, 6, 8,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
39 9,10,11,12,13,14,15,16,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
40 };
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
41
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
42 static const uint8_t svcd_scan_offset_placeholder[14] = {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
43 0x10, 0x0E,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
44 0x00, 0x80, 0x81,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
45 0x00, 0x80, 0x81,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
46 0xff, 0xff, 0xff,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
47 0xff, 0xff, 0xff,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
48 };
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
49
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
50 static void mpeg1_encode_block(MpegEncContext *s,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
51 DCTELEM *block,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
52 int component);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
53 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
54
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
55 static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
56 static uint8_t fcode_tab[MAX_MV*2+1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
57
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
58 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
59 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
60
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
61 /* simple include everything table for dc, first byte is bits number next 3 are code*/
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
62 static uint32_t mpeg1_lum_dc_uni[512];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
63 static uint32_t mpeg1_chr_dc_uni[512];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
64
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
65 static uint8_t mpeg1_index_run[2][64];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
66 static int8_t mpeg1_max_level[2][64];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
67
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
68 static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
69 int i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
70
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
71 for(i=0; i<128; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
72 int level= i-64;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
73 int run;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
74 for(run=0; run<64; run++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
75 int len, bits, code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
76
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
77 int alevel= FFABS(level);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
78 int sign= (level>>31)&1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
79
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
80 if (alevel > rl->max_level[0][run])
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
81 code= 111; /*rl->n*/
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
82 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
83 code= rl->index_run[0][run] + alevel - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
84
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
85 if (code < 111 /* rl->n */) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
86 /* store the vlc & sign at once */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
87 len= rl->table_vlc[code][1]+1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
88 bits= (rl->table_vlc[code][0]<<1) + sign;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
89 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
90 len= rl->table_vlc[111/*rl->n*/][1]+6;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
91 bits= rl->table_vlc[111/*rl->n*/][0]<<6;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
92
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
93 bits|= run;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
94 if (alevel < 128) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
95 bits<<=8; len+=8;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
96 bits|= level & 0xff;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
97 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
98 bits<<=16; len+=16;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
99 bits|= level & 0xff;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
100 if (level < 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
101 bits|= 0x8001 + level + 255;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
102 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
103 bits|= level & 0xffff;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
104 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
105 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
106 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
107
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
108 uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
109 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
110 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
111 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
112
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
113
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
114 static int find_frame_rate_index(MpegEncContext *s){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
115 int i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
116 int64_t dmin= INT64_MAX;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
117 int64_t d;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
118
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
119 for(i=1;i<14;i++) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
120 int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
121 int64_t n1= 1001LL*s->avctx->time_base.den;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
122 if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
123
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
124 d = FFABS(n0 - n1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
125 if(d < dmin){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
126 dmin=d;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
127 s->frame_rate_index= i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
128 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
129 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
130 if(dmin)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
131 return -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
132 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
133 return 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
134 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
135
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6481
diff changeset
136 static av_cold int encode_init(AVCodecContext *avctx)
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
137 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
138 MpegEncContext *s = avctx->priv_data;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
139
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
140 if(MPV_encode_init(avctx) < 0)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
141 return -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
142
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
143 if(find_frame_rate_index(s) < 0){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
144 if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
145 av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
146 return -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
147 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
148 av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
149 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
150 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
151
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
152 if(avctx->profile == FF_PROFILE_UNKNOWN){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
153 if(avctx->level != FF_LEVEL_UNKNOWN){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
154 av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
155 return -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
156 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
157 avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
158 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
159
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
160 if(avctx->level == FF_LEVEL_UNKNOWN){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
161 if(avctx->profile == 0){ /* 4:2:2 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
162 if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
163 else avctx->level = 2; /* High */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
164 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
165 if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
166 av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
167 return -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
168 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
169 if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
170 else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
171 else avctx->level = 4; /* High */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
172 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
173 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
174
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
175 if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
176 av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
177 return -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
178 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
179
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
180 return 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
181 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
182
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
183 static void put_header(MpegEncContext *s, int header)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
184 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
185 align_put_bits(&s->pb);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
186 put_bits(&s->pb, 16, header>>16);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
187 put_bits(&s->pb, 16, header&0xFFFF);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
188 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
189
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
190 /* put sequence header if needed */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
191 static void mpeg1_encode_sequence_header(MpegEncContext *s)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
192 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
193 unsigned int vbv_buffer_size;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
194 unsigned int fps, v;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
195 int i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
196 uint64_t time_code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
197 float best_aspect_error= 1E10;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
198 float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
199 int constraint_parameter_flag;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
200
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
201 if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
202
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
203 if (s->current_picture.key_frame) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
204 AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
205
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
206 /* mpeg1 header repeated every gop */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
207 put_header(s, SEQ_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
209 put_bits(&s->pb, 12, s->width);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
210 put_bits(&s->pb, 12, s->height);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
211
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
212 for(i=1; i<15; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
213 float error= aspect_ratio;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
214 if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
215 error-= 1.0/ff_mpeg1_aspect[i];
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
216 else
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
217 error-= av_q2d(ff_mpeg2_aspect[i])*s->height/s->width;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
218
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
219 error= FFABS(error);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
220
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
221 if(error < best_aspect_error){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
222 best_aspect_error= error;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
223 s->aspect_ratio_info= i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
224 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
225 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
226
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
227 put_bits(&s->pb, 4, s->aspect_ratio_info);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
228 put_bits(&s->pb, 4, s->frame_rate_index);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
229
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
230 if(s->avctx->rc_max_rate){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
231 v = (s->avctx->rc_max_rate + 399) / 400;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
232 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
233 v = 0x3ffff;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
234 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
235 v= 0x3FFFF;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
236 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
237
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
238 if(s->avctx->rc_buffer_size)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
239 vbv_buffer_size = s->avctx->rc_buffer_size;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
240 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
241 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
242 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
243 vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
244
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
245 put_bits(&s->pb, 18, v & 0x3FFFF);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
246 put_bits(&s->pb, 1, 1); /* marker */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
247 put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
248
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
249 constraint_parameter_flag=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
250 s->width <= 768 && s->height <= 576 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
251 s->mb_width * s->mb_height <= 396 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
252 s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
253 framerate.num <= framerate.den*30 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
254 s->avctx->me_range && s->avctx->me_range < 128 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
255 vbv_buffer_size <= 20 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
256 v <= 1856000/400 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
257 s->codec_id == CODEC_ID_MPEG1VIDEO;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
258
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
259 put_bits(&s->pb, 1, constraint_parameter_flag);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
260
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
261 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
262 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
263
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
264 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
265 put_header(s, EXT_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
266 put_bits(&s->pb, 4, 1); //seq ext
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
267
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
268 put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
269
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
270 put_bits(&s->pb, 3, s->avctx->profile); //profile
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
271 put_bits(&s->pb, 4, s->avctx->level); //level
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
272
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
273 put_bits(&s->pb, 1, s->progressive_sequence);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
274 put_bits(&s->pb, 2, s->chroma_format);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
275 put_bits(&s->pb, 2, 0); //horizontal size ext
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
276 put_bits(&s->pb, 2, 0); //vertical size ext
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
277 put_bits(&s->pb, 12, v>>18); //bitrate ext
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
278 put_bits(&s->pb, 1, 1); //marker
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
279 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
280 put_bits(&s->pb, 1, s->low_delay);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
281 put_bits(&s->pb, 2, 0); // frame_rate_ext_n
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
282 put_bits(&s->pb, 5, 0); // frame_rate_ext_d
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
283 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
284
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
285 put_header(s, GOP_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
286 put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
287 /* time code : we must convert from the real frame rate to a
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
288 fake mpeg frame rate in case of low frame rate */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
289 fps = (framerate.num + framerate.den/2)/ framerate.den;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
290 time_code = s->current_picture_ptr->coded_picture_number + s->avctx->timecode_frame_start;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
291
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
292 s->gop_picture_number = s->current_picture_ptr->coded_picture_number;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
293 if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
294 /* only works for NTSC 29.97 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
295 int d = time_code / 17982;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
296 int m = time_code % 17982;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
297 //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
298 time_code += 18 * d + 2 * ((m - 2) / 1798);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
299 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
300 put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
301 put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
302 put_bits(&s->pb, 1, 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
303 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
304 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
305 put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
306 put_bits(&s->pb, 1, 0); /* broken link */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
307 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
308 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
309
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
310 static inline void encode_mb_skip_run(MpegEncContext *s, int run){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
311 while (run >= 33) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
312 put_bits(&s->pb, 11, 0x008);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
313 run -= 33;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
314 }
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
315 put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
316 ff_mpeg12_mbAddrIncrTable[run][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
317 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
318
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
319 static av_always_inline void put_qscale(MpegEncContext *s)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
320 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
321 if(s->q_scale_type){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
322 assert(s->qscale>=1 && s->qscale <=12);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
323 put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
324 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
325 put_bits(&s->pb, 5, s->qscale);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
326 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
327 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
328
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
329 void ff_mpeg1_encode_slice_header(MpegEncContext *s){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
330 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
331 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
332 put_bits(&s->pb, 1, 0); /* slice extra information */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
333 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
334
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
335 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
336 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
337 mpeg1_encode_sequence_header(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
338
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
339 /* mpeg1 picture header */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
340 put_header(s, PICTURE_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
341 /* temporal reference */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
342
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
343 // RAL: s->picture_number instead of s->fake_picture_number
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
344 put_bits(&s->pb, 10, (s->picture_number -
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
345 s->gop_picture_number) & 0x3ff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
346 put_bits(&s->pb, 3, s->pict_type);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
347
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
348 s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
349 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
350
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
351 // RAL: Forward f_code also needed for B frames
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
352 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
353 put_bits(&s->pb, 1, 0); /* half pel coordinates */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
354 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
355 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
356 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
357 put_bits(&s->pb, 3, 7); /* forward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
358 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
359
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
360 // RAL: Backward f_code necessary for B frames
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
361 if (s->pict_type == FF_B_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
362 put_bits(&s->pb, 1, 0); /* half pel coordinates */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
363 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
364 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
365 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
366 put_bits(&s->pb, 3, 7); /* backward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
367 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
368
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
369 put_bits(&s->pb, 1, 0); /* extra bit picture */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
370
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
371 s->frame_pred_frame_dct = 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
372 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
373 put_header(s, EXT_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
374 put_bits(&s->pb, 4, 8); //pic ext
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
375 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
376 put_bits(&s->pb, 4, s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
377 put_bits(&s->pb, 4, s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
378 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
379 put_bits(&s->pb, 8, 255);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
380 }
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
381 if (s->pict_type == FF_B_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
382 put_bits(&s->pb, 4, s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
383 put_bits(&s->pb, 4, s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
384 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
385 put_bits(&s->pb, 8, 255);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
386 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
387 put_bits(&s->pb, 2, s->intra_dc_precision);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
388
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
389 assert(s->picture_structure == PICT_FRAME);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
390 put_bits(&s->pb, 2, s->picture_structure);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
391 if (s->progressive_sequence) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
392 put_bits(&s->pb, 1, 0); /* no repeat */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
393 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
394 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
395 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
396 /* XXX: optimize the generation of this flag with entropy
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
397 measures */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
398 s->frame_pred_frame_dct = s->progressive_sequence;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
399
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
400 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
401 put_bits(&s->pb, 1, s->concealment_motion_vectors);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
402 put_bits(&s->pb, 1, s->q_scale_type);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
403 put_bits(&s->pb, 1, s->intra_vlc_format);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
404 put_bits(&s->pb, 1, s->alternate_scan);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
405 put_bits(&s->pb, 1, s->repeat_first_field);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
406 s->progressive_frame = s->progressive_sequence;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
407 put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
408 put_bits(&s->pb, 1, s->progressive_frame);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
409 put_bits(&s->pb, 1, 0); //composite_display_flag
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
410 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
411 if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
412 int i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
413
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
414 put_header(s, USER_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
415 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
416 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
417 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
418 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
419
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
420 s->mb_y=0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
421 ff_mpeg1_encode_slice_header(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
422 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
423
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
424 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
425 int has_mv, int field_motion)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
426 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
427 put_bits(&s->pb, n, bits);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
428 if (!s->frame_pred_frame_dct) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
429 if (has_mv)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
430 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
431 put_bits(&s->pb, 1, s->interlaced_dct);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
432 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
433 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
434
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
435 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
436 DCTELEM block[6][64],
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
437 int motion_x, int motion_y,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
438 int mb_block_count)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
439 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
440 int i, cbp;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
441 const int mb_x = s->mb_x;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
442 const int mb_y = s->mb_y;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
443 const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
444
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
445 /* compute cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
446 cbp = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
447 for(i=0;i<mb_block_count;i++) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
448 if (s->block_last_index[i] >= 0)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
449 cbp |= 1 << (mb_block_count - 1 - i);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
450 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
451
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
452 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
453 (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
454 ((s->pict_type == FF_P_TYPE && (motion_x | motion_y) == 0) ||
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
455 (s->pict_type == FF_B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
456 ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
457 s->mb_skip_run++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
458 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
459 s->skip_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
460 s->misc_bits++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
461 s->last_bits++;
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
462 if(s->pict_type == FF_P_TYPE){
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
463 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
464 s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
465 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
466 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
467 if(first_mb){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
468 assert(s->mb_skip_run == 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
469 encode_mb_skip_run(s, s->mb_x);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
470 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
471 encode_mb_skip_run(s, s->mb_skip_run);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
472 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
473
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
474 if (s->pict_type == FF_I_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
475 if(s->dquant && cbp){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
476 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
477 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
478 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
479 put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
480 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
481 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
482 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
483 s->i_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
484 } else if (s->mb_intra) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
485 if(s->dquant && cbp){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
486 put_mb_modes(s, 6, 0x01, 0, 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
487 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
488 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
489 put_mb_modes(s, 5, 0x03, 0, 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
490 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
491 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
492 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
493 s->i_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
494 memset(s->last_mv, 0, sizeof(s->last_mv));
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
495 } else if (s->pict_type == FF_P_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
496 if(s->mv_type == MV_TYPE_16X16){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
497 if (cbp != 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
498 if ((motion_x|motion_y) == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
499 if(s->dquant){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
500 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
501 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
502 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
503 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
504 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
505 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
506 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
507 if(s->dquant){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
508 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
509 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
510 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
511 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
512 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
513 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
514 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
515 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
516 s->mv_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
517 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
518 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
519 put_bits(&s->pb, 3, 1); /* motion only */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
520 if (!s->frame_pred_frame_dct)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
521 put_bits(&s->pb, 2, 2); /* motion_type: frame */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
522 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
523 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
524 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
525 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
526 s->mv_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
527 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
528 s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
529 s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
530 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
531 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
532
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
533 if (cbp) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
534 if(s->dquant){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
535 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
536 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
537 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
538 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
539 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
540 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
541 put_bits(&s->pb, 3, 1); /* motion only */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
542 put_bits(&s->pb, 2, 1); /* motion_type: field */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
543 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
544 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
545 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
546 for(i=0; i<2; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
547 put_bits(&s->pb, 1, s->field_select[0][i]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
548 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
549 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
550 s->last_mv[0][i][0]= s->mv[0][i][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
551 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
552 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
553 s->mv_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
554 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
555 if(cbp) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
556 if (s->chroma_y_shift) {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
557 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
558 } else {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
559 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
560 put_bits(&s->pb, 2, cbp & 3);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
561 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
562 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
563 s->f_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
564 } else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
565 if(s->mv_type == MV_TYPE_16X16){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
566 if (cbp){ // With coded bloc pattern
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
567 if (s->dquant) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
568 if(s->mv_dir == MV_DIR_FORWARD)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
569 put_mb_modes(s, 6, 3, 1, 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
570 else
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
571 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
572 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
573 } else {
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
574 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
575 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
576 }else{ // No coded bloc pattern
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
577 put_bits(&s->pb, 5-s->mv_dir, 2);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
578 if (!s->frame_pred_frame_dct)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
579 put_bits(&s->pb, 2, 2); /* motion_type: frame */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
580 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
581 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
582 s->misc_bits += get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
583 if (s->mv_dir&MV_DIR_FORWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
584 mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
585 mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
586 s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
587 s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
588 s->f_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
589 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
590 if (s->mv_dir&MV_DIR_BACKWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
591 mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
592 mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
593 s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
594 s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
595 s->b_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
596 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
597 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
598 assert(s->mv_type == MV_TYPE_FIELD);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
599 assert(!s->frame_pred_frame_dct);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
600 if (cbp){ // With coded bloc pattern
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
601 if (s->dquant) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
602 if(s->mv_dir == MV_DIR_FORWARD)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
603 put_mb_modes(s, 6, 3, 1, 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
604 else
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
605 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
606 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
607 } else {
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
608 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
609 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
610 }else{ // No coded bloc pattern
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
611 put_bits(&s->pb, 5-s->mv_dir, 2);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
612 put_bits(&s->pb, 2, 1); /* motion_type: field */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
613 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
614 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
615 s->misc_bits += get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
616 if (s->mv_dir&MV_DIR_FORWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
617 for(i=0; i<2; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
618 put_bits(&s->pb, 1, s->field_select[0][i]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
619 mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
620 mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
621 s->last_mv[0][i][0]= s->mv[0][i][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
622 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
623 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
624 s->f_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
625 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
626 if (s->mv_dir&MV_DIR_BACKWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
627 for(i=0; i<2; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
628 put_bits(&s->pb, 1, s->field_select[1][i]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
629 mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
630 mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
631 s->last_mv[1][i][0]= s->mv[1][i][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
632 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
633 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
634 s->b_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
635 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
636 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
637 s->mv_bits += get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
638 if(cbp) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
639 if (s->chroma_y_shift) {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
640 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
641 } else {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
642 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
643 put_bits(&s->pb, 2, cbp & 3);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
644 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
645 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
646 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
647 for(i=0;i<mb_block_count;i++) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
648 if (cbp & (1 << (mb_block_count - 1 - i))) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
649 mpeg1_encode_block(s, block[i], i);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
650 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
651 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
652 s->mb_skip_run = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
653 if(s->mb_intra)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
654 s->i_tex_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
655 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
656 s->p_tex_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
657 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
658 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
659
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
660 void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
661 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
662 if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
663 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
664 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
665
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
666 // RAL: Parameter added: f_or_b_code
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
667 static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
668 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
669 int code, bit_size, l, bits, range, sign;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
670
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
671 if (val == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
672 /* zero vector */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
673 code = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
674 put_bits(&s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
675 ff_mpeg12_mbMotionVectorTable[0][1],
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
676 ff_mpeg12_mbMotionVectorTable[0][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
677 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
678 bit_size = f_or_b_code - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
679 range = 1 << bit_size;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
680 /* modulo encoding */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
681 l= INT_BIT - 5 - bit_size;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
682 val= (val<<l)>>l;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
683
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
684 if (val >= 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
685 val--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
686 code = (val >> bit_size) + 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
687 bits = val & (range - 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
688 sign = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
689 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
690 val = -val;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
691 val--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
692 code = (val >> bit_size) + 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
693 bits = val & (range - 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
694 sign = 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
695 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
696
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
697 assert(code > 0 && code <= 16);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
698
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
699 put_bits(&s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
700 ff_mpeg12_mbMotionVectorTable[code][1],
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
701 ff_mpeg12_mbMotionVectorTable[code][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
702
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
703 put_bits(&s->pb, 1, sign);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
704 if (bit_size > 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
705 put_bits(&s->pb, bit_size, bits);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
706 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
707 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
708 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
709
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
710 void ff_mpeg1_encode_init(MpegEncContext *s)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
711 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
712 static int done=0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
713
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
714 ff_mpeg12_common_init(s);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
715
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
716 if(!done){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
717 int f_code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
718 int mv;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
719 int i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
720
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
721 done=1;
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
722 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
723 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
724
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
725 for(i=0; i<64; i++)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
726 {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
727 mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
728 mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
729 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
730
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
731 init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
732 if(s->intra_vlc_format)
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
733 init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
734
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
735 /* build unified dc encoding tables */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
736 for(i=-255; i<256; i++)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
737 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
738 int adiff, index;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
739 int bits, code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
740 int diff=i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
741
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
742 adiff = FFABS(diff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
743 if(diff<0) diff--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
744 index = av_log2(2*adiff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
745
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
746 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
747 code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
748 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
749
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
750 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
751 code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
752 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
753 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
754
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
755 for(f_code=1; f_code<=MAX_FCODE; f_code++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
756 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
757 int len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
758
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
759 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
760 else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
761 int val, bit_size, range, code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
762
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
763 bit_size = f_code - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
764 range = 1 << bit_size;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
765
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
766 val=mv;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
767 if (val < 0)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
768 val = -val;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
769 val--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
770 code = (val >> bit_size) + 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
771 if(code<17){
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
772 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
773 }else{
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
774 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
775 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
776 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
777
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
778 mv_penalty[f_code][mv+MAX_MV]= len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
779 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
780 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
781
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
782
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
783 for(f_code=MAX_FCODE; f_code>0; f_code--){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
784 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
785 fcode_tab[mv+MAX_MV]= f_code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
786 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
787 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
788 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
789 s->me.mv_penalty= mv_penalty;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
790 s->fcode_tab= fcode_tab;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
791 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
792 s->min_qcoeff=-255;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
793 s->max_qcoeff= 255;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
794 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
795 s->min_qcoeff=-2047;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
796 s->max_qcoeff= 2047;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
797 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
798 if (s->intra_vlc_format) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
799 s->intra_ac_vlc_length=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
800 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
801 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
802 s->intra_ac_vlc_length=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
803 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
804 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
805 s->inter_ac_vlc_length=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
806 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
807 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
808
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
809 static inline void encode_dc(MpegEncContext *s, int diff, int component)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
810 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
811 if(((unsigned) (diff+255)) >= 511){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
812 int index;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
813
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
814 if(diff<0){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
815 index= av_log2_16bit(-2*diff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
816 diff--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
817 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
818 index= av_log2_16bit(2*diff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
819 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
820 if (component == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
821 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
822 &s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
823 ff_mpeg12_vlc_dc_lum_bits[index] + index,
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
824 (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
825 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
826 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
827 &s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
828 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
829 (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
830 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
831 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
832 if (component == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
833 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
834 &s->pb,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
835 mpeg1_lum_dc_uni[diff+255]&0xFF,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
836 mpeg1_lum_dc_uni[diff+255]>>8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
837 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
838 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
839 &s->pb,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
840 mpeg1_chr_dc_uni[diff+255]&0xFF,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
841 mpeg1_chr_dc_uni[diff+255]>>8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
842 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
843 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
844 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
845
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
846 static void mpeg1_encode_block(MpegEncContext *s,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
847 DCTELEM *block,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
848 int n)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
849 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
850 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
851 int code, component;
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
852 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
853
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
854 last_index = s->block_last_index[n];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
855
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
856 /* DC coef */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
857 if (s->mb_intra) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
858 component = (n <= 3 ? 0 : (n&1) + 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
859 dc = block[0]; /* overflow is impossible */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
860 diff = dc - s->last_dc[component];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
861 encode_dc(s, diff, component);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
862 s->last_dc[component] = dc;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
863 i = 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
864 if (s->intra_vlc_format)
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
865 table_vlc = ff_rl_mpeg2.table_vlc;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
866 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
867 /* encode the first coefficient : needs to be done here because
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
868 it is handled slightly differently */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
869 level = block[0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
870 if (abs(level) == 1) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
871 code = ((uint32_t)level >> 31); /* the sign bit */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
872 put_bits(&s->pb, 2, code | 0x02);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
873 i = 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
874 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
875 i = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
876 last_non_zero = -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
877 goto next_coef;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
878 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
879 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
880
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
881 /* now quantify & encode AC coefs */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
882 last_non_zero = i - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
883
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
884 for(;i<=last_index;i++) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
885 j = s->intra_scantable.permutated[i];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
886 level = block[j];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
887 next_coef:
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
888 #if 0
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
889 if (level != 0)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
890 dprintf(s->avctx, "level[%d]=%d\n", i, level);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
891 #endif
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
892 /* encode using VLC */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
893 if (level != 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
894 run = i - last_non_zero - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
895
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
896 alevel= level;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
897 MASK_ABS(sign, alevel)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
898 sign&=1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
899
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
900 if (alevel <= mpeg1_max_level[0][run]){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
901 code= mpeg1_index_run[0][run] + alevel - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
902 /* store the vlc & sign at once */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
903 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
904 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
905 /* escape seems to be pretty rare <5% so I do not optimize it */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
906 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
907 /* escape: only clip in this case */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
908 put_bits(&s->pb, 6, run);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
909 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
910 if (alevel < 128) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
911 put_bits(&s->pb, 8, level & 0xff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
912 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
913 if (level < 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
914 put_bits(&s->pb, 16, 0x8001 + level + 255);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
915 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
916 put_bits(&s->pb, 16, level & 0xffff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
917 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
918 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
919 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
920 put_bits(&s->pb, 12, level & 0xfff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
921 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
922 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
923 last_non_zero = i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
924 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
925 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
926 /* end of block */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
927 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
928 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
929
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
930 AVCodec mpeg1video_encoder = {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
931 "mpeg1video",
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
932 CODEC_TYPE_VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
933 CODEC_ID_MPEG1VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
934 sizeof(MpegEncContext),
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
935 encode_init,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
936 MPV_encode_picture,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
937 MPV_encode_end,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
938 .supported_framerates= ff_frame_rate_tab+1,
6788
e1302edb0f69 Replace some occurrences of -1 with PIX_FMT_NONE.
cehoyos
parents: 6717
diff changeset
939 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
940 .capabilities= CODEC_CAP_DELAY,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
941 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
942 };
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
943
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
944 AVCodec mpeg2video_encoder = {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
945 "mpeg2video",
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
946 CODEC_TYPE_VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
947 CODEC_ID_MPEG2VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
948 sizeof(MpegEncContext),
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
949 encode_init,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
950 MPV_encode_picture,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
951 MPV_encode_end,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
952 .supported_framerates= ff_frame_rate_tab+1,
6788
e1302edb0f69 Replace some occurrences of -1 with PIX_FMT_NONE.
cehoyos
parents: 6717
diff changeset
953 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
954 .capabilities= CODEC_CAP_DELAY,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
955 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
956 };