annotate mpeg12enc.c @ 11480:534872e7ab38 libavcodec

Make iff.c:decode_init return the value returned by avctx->get_buffer() in case of error, rather than AVERROR_UNKNOWN which is deprecated, and mark AVERROR_UNKNOWN for deletion at the next major bump.
author stefano
date Sat, 13 Mar 2010 19:19:40 +0000
parents 9dcd906bd334
children 8a4984c5cacc
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
8629
04423b2f6e0b cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 7260
diff changeset
3 * Copyright (c) 2000,2001 Fabrice Bellard
5208
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 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8629
diff changeset
24 * @file libavcodec/mpeg12enc.c
5208
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);
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
187 put_sbits(&s->pb, 16, header);
5208
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
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
209 put_sbits(&s->pb, 12, s->width );
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
210 put_sbits(&s->pb, 12, s->height);
5208
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
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
245 put_sbits(&s->pb, 18, v);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
246 put_bits(&s->pb, 1, 1); /* marker */
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
247 put_sbits(&s->pb, 10, vbv_buffer_size);
5208
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);
7143
8e25ae9f1c4a Support >=4096 dimensions, fixes issue499.
michael
parents: 7040
diff changeset
275 put_bits(&s->pb, 2, s->width >>12);
8e25ae9f1c4a Support >=4096 dimensions, fixes issue499.
michael
parents: 7040
diff changeset
276 put_bits(&s->pb, 2, s->height>>12);
5208
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){
10195
82ce5ee72165 Allow encoding frames with more than 2800 lines.
cehoyos
parents: 10146
diff changeset
330 if (s->height > 2800) {
82ce5ee72165 Allow encoding frames with more than 2800 lines.
cehoyos
parents: 10146
diff changeset
331 put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
82ce5ee72165 Allow encoding frames with more than 2800 lines.
cehoyos
parents: 10146
diff changeset
332 put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */
82ce5ee72165 Allow encoding frames with more than 2800 lines.
cehoyos
parents: 10146
diff changeset
333 } else {
10196
9dcd906bd334 Cosmetics: Reindent after r19927.
cehoyos
parents: 10195
diff changeset
334 put_header(s, SLICE_MIN_START_CODE + s->mb_y);
10195
82ce5ee72165 Allow encoding frames with more than 2800 lines.
cehoyos
parents: 10146
diff changeset
335 }
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
336 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
337 put_bits(&s->pb, 1, 0); /* slice extra information */
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
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
340 void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
341 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
342 mpeg1_encode_sequence_header(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
343
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
344 /* mpeg1 picture header */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
345 put_header(s, PICTURE_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
346 /* temporal reference */
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 // RAL: s->picture_number instead of s->fake_picture_number
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
349 put_bits(&s->pb, 10, (s->picture_number -
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
350 s->gop_picture_number) & 0x3ff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
351 put_bits(&s->pb, 3, s->pict_type);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
352
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
353 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
354 put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
355
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
356 // RAL: Forward f_code also needed for B frames
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
357 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
358 put_bits(&s->pb, 1, 0); /* half pel coordinates */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
359 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
360 put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
361 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
362 put_bits(&s->pb, 3, 7); /* forward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
363 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
364
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
365 // RAL: Backward f_code necessary for B frames
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
366 if (s->pict_type == FF_B_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
367 put_bits(&s->pb, 1, 0); /* half pel coordinates */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
368 if(s->codec_id == CODEC_ID_MPEG1VIDEO)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
369 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
370 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
371 put_bits(&s->pb, 3, 7); /* backward_f_code */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
372 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
373
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
374 put_bits(&s->pb, 1, 0); /* extra bit picture */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
375
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
376 s->frame_pred_frame_dct = 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
377 if(s->codec_id == CODEC_ID_MPEG2VIDEO){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
378 put_header(s, EXT_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
379 put_bits(&s->pb, 4, 8); //pic ext
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
380 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
381 put_bits(&s->pb, 4, s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
382 put_bits(&s->pb, 4, s->f_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
383 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
384 put_bits(&s->pb, 8, 255);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
385 }
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
386 if (s->pict_type == FF_B_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
387 put_bits(&s->pb, 4, s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
388 put_bits(&s->pb, 4, s->b_code);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
389 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
390 put_bits(&s->pb, 8, 255);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
391 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
392 put_bits(&s->pb, 2, s->intra_dc_precision);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
393
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
394 assert(s->picture_structure == PICT_FRAME);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
395 put_bits(&s->pb, 2, s->picture_structure);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
396 if (s->progressive_sequence) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
397 put_bits(&s->pb, 1, 0); /* no repeat */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
398 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
399 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
400 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
401 /* XXX: optimize the generation of this flag with entropy
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
402 measures */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
403 s->frame_pred_frame_dct = s->progressive_sequence;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
404
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
405 put_bits(&s->pb, 1, s->frame_pred_frame_dct);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
406 put_bits(&s->pb, 1, s->concealment_motion_vectors);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
407 put_bits(&s->pb, 1, s->q_scale_type);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
408 put_bits(&s->pb, 1, s->intra_vlc_format);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
409 put_bits(&s->pb, 1, s->alternate_scan);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
410 put_bits(&s->pb, 1, s->repeat_first_field);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
411 s->progressive_frame = s->progressive_sequence;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
412 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
413 put_bits(&s->pb, 1, s->progressive_frame);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
414 put_bits(&s->pb, 1, 0); //composite_display_flag
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
415 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
416 if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
417 int i;
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 put_header(s, USER_START_CODE);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
420 for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
421 put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
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
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
425 s->mb_y=0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
426 ff_mpeg1_encode_slice_header(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
427 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
428
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
429 static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
430 int has_mv, int field_motion)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
431 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
432 put_bits(&s->pb, n, bits);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
433 if (!s->frame_pred_frame_dct) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
434 if (has_mv)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
435 put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
436 put_bits(&s->pb, 1, s->interlaced_dct);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
437 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
438 }
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 static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
441 DCTELEM block[6][64],
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
442 int motion_x, int motion_y,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
443 int mb_block_count)
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 int i, cbp;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
446 const int mb_x = s->mb_x;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
447 const int mb_y = s->mb_y;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
448 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
449
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
450 /* compute cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
451 cbp = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
452 for(i=0;i<mb_block_count;i++) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
453 if (s->block_last_index[i] >= 0)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
454 cbp |= 1 << (mb_block_count - 1 - i);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
455 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
456
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
457 if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
458 (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
459 ((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
460 (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
461 ((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
462 s->mb_skip_run++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
463 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
464 s->skip_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
465 s->misc_bits++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
466 s->last_bits++;
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
467 if(s->pict_type == FF_P_TYPE){
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
468 s->last_mv[0][1][0]= s->last_mv[0][0][0]=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
469 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
470 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
471 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
472 if(first_mb){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
473 assert(s->mb_skip_run == 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
474 encode_mb_skip_run(s, s->mb_x);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
475 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
476 encode_mb_skip_run(s, s->mb_skip_run);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
477 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
478
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
479 if (s->pict_type == FF_I_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
480 if(s->dquant && cbp){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
481 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
482 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
483 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
484 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
485 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
486 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
487 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
488 s->i_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
489 } else if (s->mb_intra) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
490 if(s->dquant && cbp){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
491 put_mb_modes(s, 6, 0x01, 0, 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
492 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
493 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
494 put_mb_modes(s, 5, 0x03, 0, 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
495 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
496 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
497 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
498 s->i_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
499 memset(s->last_mv, 0, sizeof(s->last_mv));
6481
493dc59d469a add FF_ prefix to all (frame)_TYPE usage
aurel
parents: 5446
diff changeset
500 } else if (s->pict_type == FF_P_TYPE) {
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
501 if(s->mv_type == MV_TYPE_16X16){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
502 if (cbp != 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
503 if ((motion_x|motion_y) == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
504 if(s->dquant){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
505 put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
506 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
507 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
508 put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
509 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
510 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
511 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
512 if(s->dquant){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
513 put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
514 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
515 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
516 put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
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 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
519 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
520 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
521 s->mv_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
522 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
523 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
524 put_bits(&s->pb, 3, 1); /* motion only */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
525 if (!s->frame_pred_frame_dct)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
526 put_bits(&s->pb, 2, 2); /* motion_type: frame */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
527 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
528 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
529 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
530 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
531 s->mv_bits+= get_bits_diff(s);
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 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
534 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
535 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
536 assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
537
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
538 if (cbp) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
539 if(s->dquant){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
540 put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
541 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
542 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
543 put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
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 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
546 put_bits(&s->pb, 3, 1); /* motion only */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
547 put_bits(&s->pb, 2, 1); /* motion_type: field */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
548 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
549 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
550 s->misc_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
551 for(i=0; i<2; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
552 put_bits(&s->pb, 1, s->field_select[0][i]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
553 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
554 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
555 s->last_mv[0][i][0]= s->mv[0][i][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
556 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
557 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
558 s->mv_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
559 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
560 if(cbp) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
561 if (s->chroma_y_shift) {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
562 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
563 } else {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
564 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
565 put_sbits(&s->pb, 2, cbp);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
566 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
567 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
568 s->f_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
569 } else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
570 if(s->mv_type == MV_TYPE_16X16){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
571 if (cbp){ // With coded bloc pattern
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
572 if (s->dquant) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
573 if(s->mv_dir == MV_DIR_FORWARD)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
574 put_mb_modes(s, 6, 3, 1, 0);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
575 else
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
576 put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
577 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
578 } else {
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
579 put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
580 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
581 }else{ // No coded bloc pattern
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
582 put_bits(&s->pb, 5-s->mv_dir, 2);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
583 if (!s->frame_pred_frame_dct)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
584 put_bits(&s->pb, 2, 2); /* motion_type: frame */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
585 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
586 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
587 s->misc_bits += get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
588 if (s->mv_dir&MV_DIR_FORWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
589 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
590 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
591 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
592 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
593 s->f_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
594 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
595 if (s->mv_dir&MV_DIR_BACKWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
596 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
597 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
598 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
599 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
600 s->b_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
601 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
602 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
603 assert(s->mv_type == MV_TYPE_FIELD);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
604 assert(!s->frame_pred_frame_dct);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
605 if (cbp){ // With coded bloc pattern
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
606 if (s->dquant) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
607 if(s->mv_dir == MV_DIR_FORWARD)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
608 put_mb_modes(s, 6, 3, 1, 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
609 else
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
610 put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
611 put_qscale(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
612 } else {
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
613 put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
5208
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 }else{ // No coded bloc pattern
5446
9af99d2cce0f get rid of mb_type_len
michael
parents: 5445
diff changeset
616 put_bits(&s->pb, 5-s->mv_dir, 2);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
617 put_bits(&s->pb, 2, 1); /* motion_type: field */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
618 s->qscale -= s->dquant;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
619 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
620 s->misc_bits += get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
621 if (s->mv_dir&MV_DIR_FORWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
622 for(i=0; i<2; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
623 put_bits(&s->pb, 1, s->field_select[0][i]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
624 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
625 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
626 s->last_mv[0][i][0]= s->mv[0][i][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
627 s->last_mv[0][i][1]= 2*s->mv[0][i][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
628 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
629 s->f_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
630 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
631 if (s->mv_dir&MV_DIR_BACKWARD){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
632 for(i=0; i<2; i++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
633 put_bits(&s->pb, 1, s->field_select[1][i]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
634 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
635 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
636 s->last_mv[1][i][0]= s->mv[1][i][0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
637 s->last_mv[1][i][1]= 2*s->mv[1][i][1];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
638 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
639 s->b_count++;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
640 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
641 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
642 s->mv_bits += get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
643 if(cbp) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
644 if (s->chroma_y_shift) {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
645 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
646 } else {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
647 put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
648 put_sbits(&s->pb, 2, cbp);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
649 }
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 for(i=0;i<mb_block_count;i++) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
653 if (cbp & (1 << (mb_block_count - 1 - i))) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
654 mpeg1_encode_block(s, block[i], i);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
655 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
656 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
657 s->mb_skip_run = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
658 if(s->mb_intra)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
659 s->i_tex_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
660 else
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
661 s->p_tex_bits+= get_bits_diff(s);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
662 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
663 }
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 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
666 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
667 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
668 else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
669 }
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 // RAL: Parameter added: f_or_b_code
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
672 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
673 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
674 if (val == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
675 /* zero vector */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
676 put_bits(&s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
677 ff_mpeg12_mbMotionVectorTable[0][1],
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
678 ff_mpeg12_mbMotionVectorTable[0][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
679 } else {
9457
6fe0b0ff991a Move declarations in mpeg1_encode_motion() closer to where they are needed.
michael
parents: 9456
diff changeset
680 int code, sign, bits;
6fe0b0ff991a Move declarations in mpeg1_encode_motion() closer to where they are needed.
michael
parents: 9456
diff changeset
681 int bit_size = f_or_b_code - 1;
6fe0b0ff991a Move declarations in mpeg1_encode_motion() closer to where they are needed.
michael
parents: 9456
diff changeset
682 int range = 1 << bit_size;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
683 /* modulo encoding */
9457
6fe0b0ff991a Move declarations in mpeg1_encode_motion() closer to where they are needed.
michael
parents: 9456
diff changeset
684 int l= INT_BIT - 5 - bit_size;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
685 val= (val<<l)>>l;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
686
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
687 if (val >= 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
688 val--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
689 code = (val >> bit_size) + 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
690 bits = val & (range - 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
691 sign = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
692 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
693 val = -val;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
694 val--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
695 code = (val >> bit_size) + 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
696 bits = val & (range - 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
697 sign = 1;
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
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
700 assert(code > 0 && code <= 16);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
701
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
702 put_bits(&s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
703 ff_mpeg12_mbMotionVectorTable[code][1],
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
704 ff_mpeg12_mbMotionVectorTable[code][0]);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
705
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
706 put_bits(&s->pb, 1, sign);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
707 if (bit_size > 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
708 put_bits(&s->pb, bit_size, bits);
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 }
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
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
713 void ff_mpeg1_encode_init(MpegEncContext *s)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
714 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
715 static int done=0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
716
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
717 ff_mpeg12_common_init(s);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
718
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
719 if(!done){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
720 int f_code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
721 int mv;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
722 int i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
723
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
724 done=1;
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
725 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
726 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
727
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
728 for(i=0; i<64; i++)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
729 {
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
730 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
731 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
732 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
733
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
734 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
735 if(s->intra_vlc_format)
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
736 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
737
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
738 /* build unified dc encoding tables */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
739 for(i=-255; i<256; i++)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
740 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
741 int adiff, index;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
742 int bits, code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
743 int diff=i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
744
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
745 adiff = FFABS(diff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
746 if(diff<0) diff--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
747 index = av_log2(2*adiff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
748
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
749 bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
750 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
751 mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
752
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
753 bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
754 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
755 mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
756 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
757
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
758 for(f_code=1; f_code<=MAX_FCODE; f_code++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
759 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
760 int len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
761
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
762 if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
763 else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
764 int val, bit_size, range, code;
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 bit_size = f_code - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
767 range = 1 << bit_size;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
768
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
769 val=mv;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
770 if (val < 0)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
771 val = -val;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
772 val--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
773 code = (val >> bit_size) + 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
774 if(code<17){
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
775 len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
776 }else{
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
777 len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
778 }
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 mv_penalty[f_code][mv+MAX_MV]= len;
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 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
784
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
785
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
786 for(f_code=MAX_FCODE; f_code>0; f_code--){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
787 for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
788 fcode_tab[mv+MAX_MV]= f_code;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
789 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
790 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
791 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
792 s->me.mv_penalty= mv_penalty;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
793 s->fcode_tab= fcode_tab;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
794 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
795 s->min_qcoeff=-255;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
796 s->max_qcoeff= 255;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
797 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
798 s->min_qcoeff=-2047;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
799 s->max_qcoeff= 2047;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
800 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
801 if (s->intra_vlc_format) {
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_mpeg2_ac_vlc_len;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
804 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
805 s->intra_ac_vlc_length=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
806 s->intra_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 s->inter_ac_vlc_length=
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
809 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
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
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
812 static inline void encode_dc(MpegEncContext *s, int diff, int component)
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(((unsigned) (diff+255)) >= 511){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
815 int index;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
816
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
817 if(diff<0){
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 diff--;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
820 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
821 index= av_log2_16bit(2*diff);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
822 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
823 if (component == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
824 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
825 &s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
826 ff_mpeg12_vlc_dc_lum_bits[index] + index,
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
827 (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
828 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
829 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
830 &s->pb,
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
831 ff_mpeg12_vlc_dc_chroma_bits[index] + index,
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
832 (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
833 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
834 }else{
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
835 if (component == 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
836 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
837 &s->pb,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
838 mpeg1_lum_dc_uni[diff+255]&0xFF,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
839 mpeg1_lum_dc_uni[diff+255]>>8);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
840 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
841 put_bits(
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
842 &s->pb,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
843 mpeg1_chr_dc_uni[diff+255]&0xFF,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
844 mpeg1_chr_dc_uni[diff+255]>>8);
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 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
847 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
848
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
849 static void mpeg1_encode_block(MpegEncContext *s,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
850 DCTELEM *block,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
851 int n)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
852 {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
853 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
854 int code, component;
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
855 const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
856
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
857 last_index = s->block_last_index[n];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
858
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
859 /* DC coef */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
860 if (s->mb_intra) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
861 component = (n <= 3 ? 0 : (n&1) + 1);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
862 dc = block[0]; /* overflow is impossible */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
863 diff = dc - s->last_dc[component];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
864 encode_dc(s, diff, component);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
865 s->last_dc[component] = dc;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
866 i = 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
867 if (s->intra_vlc_format)
5210
b52fd9b223bb add proper ff_ prefix to new extern symbols
aurel
parents: 5208
diff changeset
868 table_vlc = ff_rl_mpeg2.table_vlc;
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
869 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
870 /* encode the first coefficient : needs to be done here because
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
871 it is handled slightly differently */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
872 level = block[0];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
873 if (abs(level) == 1) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
874 code = ((uint32_t)level >> 31); /* the sign bit */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
875 put_bits(&s->pb, 2, code | 0x02);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
876 i = 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
877 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
878 i = 0;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
879 last_non_zero = -1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
880 goto next_coef;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
881 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
882 }
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 /* now quantify & encode AC coefs */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
885 last_non_zero = i - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
886
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
887 for(;i<=last_index;i++) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
888 j = s->intra_scantable.permutated[i];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
889 level = block[j];
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
890 next_coef:
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
891 #if 0
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
892 if (level != 0)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
893 dprintf(s->avctx, "level[%d]=%d\n", i, level);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
894 #endif
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
895 /* encode using VLC */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
896 if (level != 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
897 run = i - last_non_zero - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
898
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
899 alevel= level;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
900 MASK_ABS(sign, alevel)
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
901 sign&=1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
902
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
903 if (alevel <= mpeg1_max_level[0][run]){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
904 code= mpeg1_index_run[0][run] + alevel - 1;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
905 /* store the vlc & sign at once */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
906 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
907 } else {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
908 /* 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
909 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
910 /* escape: only clip in this case */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
911 put_bits(&s->pb, 6, run);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
912 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
913 if (alevel < 128) {
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
914 put_sbits(&s->pb, 8, level);
5208
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 if (level < 0) {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
917 put_bits(&s->pb, 16, 0x8001 + level + 255);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
918 } else {
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
919 put_sbits(&s->pb, 16, level);
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
920 }
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 }else{
7260
3ec34b551aae bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents: 7143
diff changeset
923 put_sbits(&s->pb, 12, level);
5208
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 last_non_zero = i;
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
927 }
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 /* end of block */
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
930 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
931 }
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
932
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
933 AVCodec mpeg1video_encoder = {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
934 "mpeg1video",
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
935 CODEC_TYPE_VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
936 CODEC_ID_MPEG1VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
937 sizeof(MpegEncContext),
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
938 encode_init,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
939 MPV_encode_picture,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
940 MPV_encode_end,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
941 .supported_framerates= ff_frame_rate_tab+1,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 9457
diff changeset
942 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
943 .capabilities= CODEC_CAP_DELAY,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
944 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
945 };
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
946
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
947 AVCodec mpeg2video_encoder = {
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
948 "mpeg2video",
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
949 CODEC_TYPE_VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
950 CODEC_ID_MPEG2VIDEO,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
951 sizeof(MpegEncContext),
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
952 encode_init,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
953 MPV_encode_picture,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
954 MPV_encode_end,
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
955 .supported_framerates= ff_frame_rate_tab+1,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 9457
diff changeset
956 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
957 .capabilities= CODEC_CAP_DELAY,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
958 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
5208
430f60820b21 split mpeg12 encoder out of mpeg12.c
aurel
parents:
diff changeset
959 };