Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 1 | /* |
2 | * MPEG1/2 encoder |
||
3 | * Copyright (c) 2000,2001 Fabrice Bellard |
||
4 | * Copyright (c) 2002-2004 Michael Niedermayer |
||
5 | * |
||
6 | * This file is part of FFmpeg. |
||
7 | * |
||
8 | * FFmpeg is free software; you can redistribute it and/or |
||
9 | * modify it under the terms of the GNU Lesser General Public |
||
10 | * License as published by the Free Software Foundation; either |
||
11 | * version 2.1 of the License, or (at your option) any later version. |
||
12 | * |
||
13 | * FFmpeg is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
16 | * Lesser General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Lesser General Public |
||
19 | * License along with FFmpeg; if not, write to the Free Software |
||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||
21 | */ |
||
22 | |||
23 | /** |
||
24 | * @file |
||
25 | * MPEG1/2 encoder |
||
26 | */ |
||
27 | |||
28 | #include "libavutil/attributes.h" |
||
29 | #include "libavutil/avassert.h" |
||
30 | #include "libavutil/log.h" |
||
31 | #include "libavutil/opt.h" |
||
32 | #include "libavutil/timecode.h" |
||
33 | #include "avcodec.h" |
||
34 | #include "bytestream.h" |
||
35 | #include "mathops.h" |
||
36 | #include "mpeg12.h" |
||
37 | #include "mpeg12data.h" |
||
38 | #include "mpegvideo.h" |
||
39 | |||
40 | |||
41 | static const uint8_t inv_non_linear_qscale[] = { |
||
42 | 0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
||
43 | }; |
||
44 | |||
45 | static const uint8_t svcd_scan_offset_placeholder[] = { |
||
46 | 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80, |
||
47 | 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
||
48 | }; |
||
49 | |||
50 | static uint8_t mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1]; |
||
51 | static uint8_t fcode_tab[MAX_MV * 2 + 1]; |
||
52 | |||
53 | static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2]; |
||
54 | static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2]; |
||
55 | |||
56 | /* simple include everything table for dc, first byte is bits |
||
57 | * number next 3 are code */ |
||
58 | static uint32_t mpeg1_lum_dc_uni[512]; |
||
59 | static uint32_t mpeg1_chr_dc_uni[512]; |
||
60 | |||
61 | static uint8_t mpeg1_index_run[2][64]; |
||
62 | static int8_t mpeg1_max_level[2][64]; |
||
63 | |||
64 | static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len) |
||
65 | { |
||
66 | int i; |
||
67 | |||
68 | for (i = 0; i < 128; i++) { |
||
69 | int level = i - 64; |
||
70 | int run; |
||
71 | if (!level) |
||
72 | continue; |
||
73 | for (run = 0; run < 64; run++) { |
||
74 | int len, code; |
||
75 | int alevel = FFABS(level); |
||
76 | |||
77 | if (alevel > rl->max_level[0][run]) |
||
78 | code = 111; /* rl->n */ |
||
79 | else |
||
80 | code = rl->index_run[0][run] + alevel - 1; |
||
81 | |||
82 | if (code < 111) { /* rl->n */ |
||
83 | /* length of VLC and sign */ |
||
84 | len = rl->table_vlc[code][1] + 1; |
||
85 | } else { |
||
86 | len = rl->table_vlc[111 /* rl->n */][1] + 6; |
||
87 | |||
88 | if (alevel < 128) |
||
89 | len += 8; |
||
90 | else |
||
91 | len += 16; |
||
92 | } |
||
93 | |||
94 | uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len; |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | static int find_frame_rate_index(MpegEncContext *s) |
||
100 | { |
||
101 | int i; |
||
102 | AVRational bestq = (AVRational) {0, 0}; |
||
103 | AVRational ext; |
||
104 | AVRational target = av_inv_q(s->avctx->time_base); |
||
105 | |||
106 | for (i = 1; i < 14; i++) { |
||
107 | if (s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && |
||
108 | i >= 9) |
||
109 | break; |
||
110 | |||
111 | for (ext.num=1; ext.num <= 4; ext.num++) { |
||
112 | for (ext.den=1; ext.den <= 32; ext.den++) { |
||
113 | AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]); |
||
114 | |||
115 | if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1)) |
||
116 | continue; |
||
117 | if (av_gcd(ext.den, ext.num) != 1) |
||
118 | continue; |
||
119 | |||
120 | if ( bestq.num==0 |
||
121 | || av_nearer_q(target, bestq, q) < 0 |
||
122 | || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) { |
||
123 | bestq = q; |
||
124 | s->frame_rate_index = i; |
||
125 | s->mpeg2_frame_rate_ext.num = ext.num; |
||
126 | s->mpeg2_frame_rate_ext.den = ext.den; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | if (av_cmp_q(target, bestq)) |
||
133 | return -1; |
||
134 | else |
||
135 | return 0; |
||
136 | } |
||
137 | |||
138 | static av_cold int encode_init(AVCodecContext *avctx) |
||
139 | { |
||
140 | MpegEncContext *s = avctx->priv_data; |
||
141 | |||
142 | if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && avctx->height > 2800) |
||
143 | avctx->thread_count = 1; |
||
144 | |||
145 | if (ff_MPV_encode_init(avctx) < 0) |
||
146 | return -1; |
||
147 | |||
148 | if (find_frame_rate_index(s) < 0) { |
||
149 | if (s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { |
||
150 | av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", |
||
151 | avctx->time_base.den, avctx->time_base.num); |
||
152 | return -1; |
||
153 | } else { |
||
154 | av_log(avctx, AV_LOG_INFO, |
||
155 | "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", |
||
156 | avctx->time_base.den, avctx->time_base.num); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | if (avctx->profile == FF_PROFILE_UNKNOWN) { |
||
161 | if (avctx->level != FF_LEVEL_UNKNOWN) { |
||
162 | av_log(avctx, AV_LOG_ERROR, "Set profile and level\n"); |
||
163 | return -1; |
||
164 | } |
||
165 | /* Main or 4:2:2 */ |
||
166 | avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; |
||
167 | } |
||
168 | |||
169 | if (avctx->level == FF_LEVEL_UNKNOWN) { |
||
170 | if (avctx->profile == 0) { /* 4:2:2 */ |
||
171 | if (avctx->width <= 720 && avctx->height <= 608) |
||
172 | avctx->level = 5; /* Main */ |
||
173 | else |
||
174 | avctx->level = 2; /* High */ |
||
175 | } else { |
||
176 | if (avctx->profile != 1 && s->chroma_format != CHROMA_420) { |
||
177 | av_log(avctx, AV_LOG_ERROR, |
||
178 | "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n"); |
||
179 | return -1; |
||
180 | } |
||
181 | if (avctx->width <= 720 && avctx->height <= 576) |
||
182 | avctx->level = 8; /* Main */ |
||
183 | else if (avctx->width <= 1440) |
||
184 | avctx->level = 6; /* High 1440 */ |
||
185 | else |
||
186 | avctx->level = 4; /* High */ |
||
187 | } |
||
188 | } |
||
189 | |||
190 | if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) { |
||
191 | av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n"); |
||
192 | return AVERROR(EINVAL); |
||
193 | } |
||
194 | |||
195 | if (s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { |
||
196 | if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) { |
||
197 | av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiplies of 4096\n" |
||
198 | "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL); |
||
199 | return AVERROR(EINVAL); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | s->drop_frame_timecode = s->drop_frame_timecode || !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE); |
||
204 | if (s->drop_frame_timecode) |
||
205 | s->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME; |
||
206 | if (s->drop_frame_timecode && s->frame_rate_index != 4) { |
||
207 | av_log(avctx, AV_LOG_ERROR, |
||
208 | "Drop frame time code only allowed with 1001/30000 fps\n"); |
||
209 | return -1; |
||
210 | } |
||
211 | |||
212 | if (s->tc_opt_str) { |
||
213 | AVRational rate = ff_mpeg12_frame_rate_tab[s->frame_rate_index]; |
||
214 | int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s); |
||
215 | if (ret < 0) |
||
216 | return ret; |
||
217 | s->drop_frame_timecode = !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME); |
||
218 | s->avctx->timecode_frame_start = s->tc.start; |
||
219 | } else { |
||
220 | s->avctx->timecode_frame_start = 0; // default is -1 |
||
221 | } |
||
222 | return 0; |
||
223 | } |
||
224 | |||
225 | static void put_header(MpegEncContext *s, int header) |
||
226 | { |
||
227 | avpriv_align_put_bits(&s->pb); |
||
228 | put_bits(&s->pb, 16, header >> 16); |
||
229 | put_sbits(&s->pb, 16, header); |
||
230 | } |
||
231 | |||
232 | /* put sequence header if needed */ |
||
233 | static void mpeg1_encode_sequence_header(MpegEncContext *s) |
||
234 | { |
||
235 | unsigned int vbv_buffer_size, fps, v; |
||
236 | int i, constraint_parameter_flag; |
||
237 | uint64_t time_code; |
||
238 | float best_aspect_error = 1E10; |
||
239 | float aspect_ratio = av_q2d(s->avctx->sample_aspect_ratio); |
||
240 | |||
241 | if (aspect_ratio == 0.0) |
||
242 | aspect_ratio = 1.0; // pixel aspect 1.1 (VGA) |
||
243 | |||
244 | if (s->current_picture.f.key_frame) { |
||
245 | AVRational framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index]; |
||
246 | |||
247 | /* mpeg1 header repeated every gop */ |
||
248 | put_header(s, SEQ_START_CODE); |
||
249 | |||
250 | put_sbits(&s->pb, 12, s->width & 0xFFF); |
||
251 | put_sbits(&s->pb, 12, s->height & 0xFFF); |
||
252 | |||
253 | for (i = 1; i < 15; i++) { |
||
254 | float error = aspect_ratio; |
||
255 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1) |
||
256 | error -= 1.0 / ff_mpeg1_aspect[i]; |
||
257 | else |
||
258 | error -= av_q2d(ff_mpeg2_aspect[i]) * s->height / s->width; |
||
259 | |||
260 | error = FFABS(error); |
||
261 | |||
262 | if (error < best_aspect_error) { |
||
263 | best_aspect_error = error; |
||
264 | s->aspect_ratio_info = i; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | put_bits(&s->pb, 4, s->aspect_ratio_info); |
||
269 | put_bits(&s->pb, 4, s->frame_rate_index); |
||
270 | |||
271 | if (s->avctx->rc_max_rate) { |
||
272 | v = (s->avctx->rc_max_rate + 399) / 400; |
||
273 | if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
||
274 | v = 0x3ffff; |
||
275 | } else { |
||
276 | v = 0x3FFFF; |
||
277 | } |
||
278 | |||
279 | if (s->avctx->rc_buffer_size) |
||
280 | vbv_buffer_size = s->avctx->rc_buffer_size; |
||
281 | else |
||
282 | /* VBV calculation: Scaled so that a VCD has the proper |
||
283 | * VBV size of 40 kilobytes */ |
||
284 | vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024; |
||
285 | vbv_buffer_size = (vbv_buffer_size + 16383) / 16384; |
||
286 | |||
287 | put_sbits(&s->pb, 18, v); |
||
288 | put_bits(&s->pb, 1, 1); // marker |
||
289 | put_sbits(&s->pb, 10, vbv_buffer_size); |
||
290 | |||
291 | constraint_parameter_flag = |
||
292 | s->width <= 768 && |
||
293 | s->height <= 576 && |
||
294 | s->mb_width * s->mb_height <= 396 && |
||
295 | s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den && |
||
296 | framerate.num <= framerate.den * 30 && |
||
297 | s->avctx->me_range && |
||
298 | s->avctx->me_range < 128 && |
||
299 | vbv_buffer_size <= 20 && |
||
300 | v <= 1856000 / 400 && |
||
301 | s->codec_id == AV_CODEC_ID_MPEG1VIDEO; |
||
302 | |||
303 | put_bits(&s->pb, 1, constraint_parameter_flag); |
||
304 | |||
305 | ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix); |
||
306 | ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix); |
||
307 | |||
308 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
||
309 | put_header(s, EXT_START_CODE); |
||
310 | put_bits(&s->pb, 4, 1); // seq ext |
||
311 | |||
312 | put_bits(&s->pb, 1, s->avctx->profile == 0); // escx 1 for 4:2:2 profile |
||
313 | |||
314 | put_bits(&s->pb, 3, s->avctx->profile); // profile |
||
315 | put_bits(&s->pb, 4, s->avctx->level); // level |
||
316 | |||
317 | put_bits(&s->pb, 1, s->progressive_sequence); |
||
318 | put_bits(&s->pb, 2, s->chroma_format); |
||
319 | put_bits(&s->pb, 2, s->width >> 12); |
||
320 | put_bits(&s->pb, 2, s->height >> 12); |
||
321 | put_bits(&s->pb, 12, v >> 18); // bitrate ext |
||
322 | put_bits(&s->pb, 1, 1); // marker |
||
323 | put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext |
||
324 | put_bits(&s->pb, 1, s->low_delay); |
||
325 | put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n |
||
326 | put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d |
||
327 | } |
||
328 | |||
329 | put_header(s, GOP_START_CODE); |
||
330 | put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag |
||
331 | /* time code: we must convert from the real frame rate to a |
||
332 | * fake MPEG frame rate in case of low frame rate */ |
||
333 | fps = (framerate.num + framerate.den / 2) / framerate.den; |
||
334 | time_code = s->current_picture_ptr->f.coded_picture_number + |
||
335 | s->avctx->timecode_frame_start; |
||
336 | |||
337 | s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number; |
||
338 | |||
339 | av_assert0(s->drop_frame_timecode == !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME)); |
||
340 | if (s->drop_frame_timecode) |
||
341 | time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps); |
||
342 | |||
343 | put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); |
||
344 | put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); |
||
345 | put_bits(&s->pb, 1, 1); |
||
346 | put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60)); |
||
347 | put_bits(&s->pb, 6, (uint32_t)((time_code % fps))); |
||
348 | put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP)); |
||
349 | put_bits(&s->pb, 1, 0); // broken link |
||
350 | } |
||
351 | } |
||
352 | |||
353 | static inline void encode_mb_skip_run(MpegEncContext *s, int run) |
||
354 | { |
||
355 | while (run >= 33) { |
||
356 | put_bits(&s->pb, 11, 0x008); |
||
357 | run -= 33; |
||
358 | } |
||
359 | put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1], |
||
360 | ff_mpeg12_mbAddrIncrTable[run][0]); |
||
361 | } |
||
362 | |||
363 | static av_always_inline void put_qscale(MpegEncContext *s) |
||
364 | { |
||
365 | if (s->q_scale_type) { |
||
366 | av_assert2(s->qscale >= 1 && s->qscale <= 12); |
||
367 | put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]); |
||
368 | } else { |
||
369 | put_bits(&s->pb, 5, s->qscale); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | void ff_mpeg1_encode_slice_header(MpegEncContext *s) |
||
374 | { |
||
375 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) { |
||
376 | put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127)); |
||
377 | /* slice_vertical_position_extension */ |
||
378 | put_bits(&s->pb, 3, s->mb_y >> 7); |
||
379 | } else { |
||
380 | put_header(s, SLICE_MIN_START_CODE + s->mb_y); |
||
381 | } |
||
382 | put_qscale(s); |
||
383 | /* slice extra information */ |
||
384 | put_bits(&s->pb, 1, 0); |
||
385 | } |
||
386 | |||
387 | void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) |
||
388 | { |
||
389 | mpeg1_encode_sequence_header(s); |
||
390 | |||
391 | /* mpeg1 picture header */ |
||
392 | put_header(s, PICTURE_START_CODE); |
||
393 | /* temporal reference */ |
||
394 | |||
395 | // RAL: s->picture_number instead of s->fake_picture_number |
||
396 | put_bits(&s->pb, 10, |
||
397 | (s->picture_number - s->gop_picture_number) & 0x3ff); |
||
398 | put_bits(&s->pb, 3, s->pict_type); |
||
399 | |||
400 | s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8; |
||
401 | put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */ |
||
402 | |||
403 | // RAL: Forward f_code also needed for B-frames |
||
404 | if (s->pict_type == AV_PICTURE_TYPE_P || |
||
405 | s->pict_type == AV_PICTURE_TYPE_B) { |
||
406 | put_bits(&s->pb, 1, 0); /* half pel coordinates */ |
||
407 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
||
408 | put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ |
||
409 | else |
||
410 | put_bits(&s->pb, 3, 7); /* forward_f_code */ |
||
411 | } |
||
412 | |||
413 | // RAL: Backward f_code necessary for B-frames |
||
414 | if (s->pict_type == AV_PICTURE_TYPE_B) { |
||
415 | put_bits(&s->pb, 1, 0); /* half pel coordinates */ |
||
416 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
||
417 | put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ |
||
418 | else |
||
419 | put_bits(&s->pb, 3, 7); /* backward_f_code */ |
||
420 | } |
||
421 | |||
422 | put_bits(&s->pb, 1, 0); /* extra bit picture */ |
||
423 | |||
424 | s->frame_pred_frame_dct = 1; |
||
425 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
||
426 | put_header(s, EXT_START_CODE); |
||
427 | put_bits(&s->pb, 4, 8); /* pic ext */ |
||
428 | if (s->pict_type == AV_PICTURE_TYPE_P || |
||
429 | s->pict_type == AV_PICTURE_TYPE_B) { |
||
430 | put_bits(&s->pb, 4, s->f_code); |
||
431 | put_bits(&s->pb, 4, s->f_code); |
||
432 | } else { |
||
433 | put_bits(&s->pb, 8, 255); |
||
434 | } |
||
435 | if (s->pict_type == AV_PICTURE_TYPE_B) { |
||
436 | put_bits(&s->pb, 4, s->b_code); |
||
437 | put_bits(&s->pb, 4, s->b_code); |
||
438 | } else { |
||
439 | put_bits(&s->pb, 8, 255); |
||
440 | } |
||
441 | put_bits(&s->pb, 2, s->intra_dc_precision); |
||
442 | |||
443 | av_assert0(s->picture_structure == PICT_FRAME); |
||
444 | put_bits(&s->pb, 2, s->picture_structure); |
||
445 | if (s->progressive_sequence) |
||
446 | put_bits(&s->pb, 1, 0); /* no repeat */ |
||
447 | else |
||
448 | put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first); |
||
449 | /* XXX: optimize the generation of this flag with entropy measures */ |
||
450 | s->frame_pred_frame_dct = s->progressive_sequence; |
||
451 | |||
452 | put_bits(&s->pb, 1, s->frame_pred_frame_dct); |
||
453 | put_bits(&s->pb, 1, s->concealment_motion_vectors); |
||
454 | put_bits(&s->pb, 1, s->q_scale_type); |
||
455 | put_bits(&s->pb, 1, s->intra_vlc_format); |
||
456 | put_bits(&s->pb, 1, s->alternate_scan); |
||
457 | put_bits(&s->pb, 1, s->repeat_first_field); |
||
458 | s->progressive_frame = s->progressive_sequence; |
||
459 | /* chroma_420_type */ |
||
460 | put_bits(&s->pb, 1, s->chroma_format == |
||
461 | CHROMA_420 ? s->progressive_frame : 0); |
||
462 | put_bits(&s->pb, 1, s->progressive_frame); |
||
463 | put_bits(&s->pb, 1, 0); /* composite_display_flag */ |
||
464 | } |
||
465 | if (s->scan_offset) { |
||
466 | int i; |
||
467 | |||
468 | put_header(s, USER_START_CODE); |
||
469 | for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++) |
||
470 | put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]); |
||
471 | } |
||
472 | |||
473 | s->mb_y = 0; |
||
474 | ff_mpeg1_encode_slice_header(s); |
||
475 | } |
||
476 | |||
477 | static inline void put_mb_modes(MpegEncContext *s, int n, int bits, |
||
478 | int has_mv, int field_motion) |
||
479 | { |
||
480 | put_bits(&s->pb, n, bits); |
||
481 | if (!s->frame_pred_frame_dct) { |
||
482 | if (has_mv) |
||
483 | /* motion_type: frame/field */ |
||
484 | put_bits(&s->pb, 2, 2 - field_motion); |
||
485 | put_bits(&s->pb, 1, s->interlaced_dct); |
||
486 | } |
||
487 | } |
||
488 | |||
489 | // RAL: Parameter added: f_or_b_code |
||
490 | static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) |
||
491 | { |
||
492 | if (val == 0) { |
||
493 | /* zero vector */ |
||
494 | put_bits(&s->pb, |
||
495 | ff_mpeg12_mbMotionVectorTable[0][1], |
||
496 | ff_mpeg12_mbMotionVectorTable[0][0]); |
||
497 | } else { |
||
498 | int code, sign, bits; |
||
499 | int bit_size = f_or_b_code - 1; |
||
500 | int range = 1 << bit_size; |
||
501 | /* modulo encoding */ |
||
502 | val = sign_extend(val, 5 + bit_size); |
||
503 | |||
504 | if (val >= 0) { |
||
505 | val--; |
||
506 | code = (val >> bit_size) + 1; |
||
507 | bits = val & (range - 1); |
||
508 | sign = 0; |
||
509 | } else { |
||
510 | val = -val; |
||
511 | val--; |
||
512 | code = (val >> bit_size) + 1; |
||
513 | bits = val & (range - 1); |
||
514 | sign = 1; |
||
515 | } |
||
516 | |||
517 | av_assert2(code > 0 && code <= 16); |
||
518 | |||
519 | put_bits(&s->pb, |
||
520 | ff_mpeg12_mbMotionVectorTable[code][1], |
||
521 | ff_mpeg12_mbMotionVectorTable[code][0]); |
||
522 | |||
523 | put_bits(&s->pb, 1, sign); |
||
524 | if (bit_size > 0) |
||
525 | put_bits(&s->pb, bit_size, bits); |
||
526 | } |
||
527 | } |
||
528 | |||
529 | static inline void encode_dc(MpegEncContext *s, int diff, int component) |
||
530 | { |
||
531 | if (((unsigned) (diff + 255)) >= 511) { |
||
532 | int index; |
||
533 | |||
534 | if (diff < 0) { |
||
535 | index = av_log2_16bit(-2 * diff); |
||
536 | diff--; |
||
537 | } else { |
||
538 | index = av_log2_16bit(2 * diff); |
||
539 | } |
||
540 | if (component == 0) |
||
541 | put_bits(&s->pb, |
||
542 | ff_mpeg12_vlc_dc_lum_bits[index] + index, |
||
543 | (ff_mpeg12_vlc_dc_lum_code[index] << index) + |
||
544 | (diff & ((1 << index) - 1))); |
||
545 | else |
||
546 | put_bits(&s->pb, |
||
547 | ff_mpeg12_vlc_dc_chroma_bits[index] + index, |
||
548 | (ff_mpeg12_vlc_dc_chroma_code[index] << index) + |
||
549 | (diff & ((1 << index) - 1))); |
||
550 | } else { |
||
551 | if (component == 0) |
||
552 | put_bits(&s->pb, |
||
553 | mpeg1_lum_dc_uni[diff + 255] & 0xFF, |
||
554 | mpeg1_lum_dc_uni[diff + 255] >> 8); |
||
555 | else |
||
556 | put_bits(&s->pb, |
||
557 | mpeg1_chr_dc_uni[diff + 255] & 0xFF, |
||
558 | mpeg1_chr_dc_uni[diff + 255] >> 8); |
||
559 | } |
||
560 | } |
||
561 | |||
562 | static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n) |
||
563 | { |
||
564 | int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; |
||
565 | int code, component; |
||
566 | const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc; |
||
567 | |||
568 | last_index = s->block_last_index[n]; |
||
569 | |||
570 | /* DC coef */ |
||
571 | if (s->mb_intra) { |
||
572 | component = (n <= 3 ? 0 : (n & 1) + 1); |
||
573 | dc = block[0]; /* overflow is impossible */ |
||
574 | diff = dc - s->last_dc[component]; |
||
575 | encode_dc(s, diff, component); |
||
576 | s->last_dc[component] = dc; |
||
577 | i = 1; |
||
578 | if (s->intra_vlc_format) |
||
579 | table_vlc = ff_rl_mpeg2.table_vlc; |
||
580 | } else { |
||
581 | /* encode the first coefficient: needs to be done here because |
||
582 | * it is handled slightly differently */ |
||
583 | level = block[0]; |
||
584 | if (abs(level) == 1) { |
||
585 | code = ((uint32_t)level >> 31); /* the sign bit */ |
||
586 | put_bits(&s->pb, 2, code | 0x02); |
||
587 | i = 1; |
||
588 | } else { |
||
589 | i = 0; |
||
590 | last_non_zero = -1; |
||
591 | goto next_coef; |
||
592 | } |
||
593 | } |
||
594 | |||
595 | /* now quantify & encode AC coefs */ |
||
596 | last_non_zero = i - 1; |
||
597 | |||
598 | for (; i <= last_index; i++) { |
||
599 | j = s->intra_scantable.permutated[i]; |
||
600 | level = block[j]; |
||
601 | |||
602 | next_coef: |
||
603 | /* encode using VLC */ |
||
604 | if (level != 0) { |
||
605 | run = i - last_non_zero - 1; |
||
606 | |||
607 | alevel = level; |
||
608 | MASK_ABS(sign, alevel); |
||
609 | sign &= 1; |
||
610 | |||
611 | if (alevel <= mpeg1_max_level[0][run]) { |
||
612 | code = mpeg1_index_run[0][run] + alevel - 1; |
||
613 | /* store the VLC & sign at once */ |
||
614 | put_bits(&s->pb, table_vlc[code][1] + 1, |
||
615 | (table_vlc[code][0] << 1) + sign); |
||
616 | } else { |
||
617 | /* escape seems to be pretty rare <5% so I do not optimize it */ |
||
618 | put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]); |
||
619 | /* escape: only clip in this case */ |
||
620 | put_bits(&s->pb, 6, run); |
||
621 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) { |
||
622 | if (alevel < 128) { |
||
623 | put_sbits(&s->pb, 8, level); |
||
624 | } else { |
||
625 | if (level < 0) |
||
626 | put_bits(&s->pb, 16, 0x8001 + level + 255); |
||
627 | else |
||
628 | put_sbits(&s->pb, 16, level); |
||
629 | } |
||
630 | } else { |
||
631 | put_sbits(&s->pb, 12, level); |
||
632 | } |
||
633 | } |
||
634 | last_non_zero = i; |
||
635 | } |
||
636 | } |
||
637 | /* end of block */ |
||
638 | put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]); |
||
639 | } |
||
640 | |||
641 | static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, |
||
642 | int16_t block[6][64], |
||
643 | int motion_x, int motion_y, |
||
644 | int mb_block_count) |
||
645 | { |
||
646 | int i, cbp; |
||
647 | const int mb_x = s->mb_x; |
||
648 | const int mb_y = s->mb_y; |
||
649 | const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y; |
||
650 | |||
651 | /* compute cbp */ |
||
652 | cbp = 0; |
||
653 | for (i = 0; i < mb_block_count; i++) |
||
654 | if (s->block_last_index[i] >= 0) |
||
655 | cbp |= 1 << (mb_block_count - 1 - i); |
||
656 | |||
657 | if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 && |
||
658 | (mb_x != s->mb_width - 1 || |
||
659 | (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) && |
||
660 | ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) || |
||
661 | (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir && |
||
662 | (((s->mv_dir & MV_DIR_FORWARD) |
||
663 | ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) | |
||
664 | (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | |
||
665 | ((s->mv_dir & MV_DIR_BACKWARD) |
||
666 | ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) | |
||
667 | (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { |
||
668 | s->mb_skip_run++; |
||
669 | s->qscale -= s->dquant; |
||
670 | s->skip_count++; |
||
671 | s->misc_bits++; |
||
672 | s->last_bits++; |
||
673 | if (s->pict_type == AV_PICTURE_TYPE_P) { |
||
674 | s->last_mv[0][0][0] = |
||
675 | s->last_mv[0][0][1] = |
||
676 | s->last_mv[0][1][0] = |
||
677 | s->last_mv[0][1][1] = 0; |
||
678 | } |
||
679 | } else { |
||
680 | if (first_mb) { |
||
681 | av_assert0(s->mb_skip_run == 0); |
||
682 | encode_mb_skip_run(s, s->mb_x); |
||
683 | } else { |
||
684 | encode_mb_skip_run(s, s->mb_skip_run); |
||
685 | } |
||
686 | |||
687 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
||
688 | if (s->dquant && cbp) { |
||
689 | /* macroblock_type: macroblock_quant = 1 */ |
||
690 | put_mb_modes(s, 2, 1, 0, 0); |
||
691 | put_qscale(s); |
||
692 | } else { |
||
693 | /* macroblock_type: macroblock_quant = 0 */ |
||
694 | put_mb_modes(s, 1, 1, 0, 0); |
||
695 | s->qscale -= s->dquant; |
||
696 | } |
||
697 | s->misc_bits += get_bits_diff(s); |
||
698 | s->i_count++; |
||
699 | } else if (s->mb_intra) { |
||
700 | if (s->dquant && cbp) { |
||
701 | put_mb_modes(s, 6, 0x01, 0, 0); |
||
702 | put_qscale(s); |
||
703 | } else { |
||
704 | put_mb_modes(s, 5, 0x03, 0, 0); |
||
705 | s->qscale -= s->dquant; |
||
706 | } |
||
707 | s->misc_bits += get_bits_diff(s); |
||
708 | s->i_count++; |
||
709 | memset(s->last_mv, 0, sizeof(s->last_mv)); |
||
710 | } else if (s->pict_type == AV_PICTURE_TYPE_P) { |
||
711 | if (s->mv_type == MV_TYPE_16X16) { |
||
712 | if (cbp != 0) { |
||
713 | if ((motion_x | motion_y) == 0) { |
||
714 | if (s->dquant) { |
||
715 | /* macroblock_pattern & quant */ |
||
716 | put_mb_modes(s, 5, 1, 0, 0); |
||
717 | put_qscale(s); |
||
718 | } else { |
||
719 | /* macroblock_pattern only */ |
||
720 | put_mb_modes(s, 2, 1, 0, 0); |
||
721 | } |
||
722 | s->misc_bits += get_bits_diff(s); |
||
723 | } else { |
||
724 | if (s->dquant) { |
||
725 | put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */ |
||
726 | put_qscale(s); |
||
727 | } else { |
||
728 | put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */ |
||
729 | } |
||
730 | s->misc_bits += get_bits_diff(s); |
||
731 | // RAL: f_code parameter added |
||
732 | mpeg1_encode_motion(s, |
||
733 | motion_x - s->last_mv[0][0][0], |
||
734 | s->f_code); |
||
735 | // RAL: f_code parameter added |
||
736 | mpeg1_encode_motion(s, |
||
737 | motion_y - s->last_mv[0][0][1], |
||
738 | s->f_code); |
||
739 | s->mv_bits += get_bits_diff(s); |
||
740 | } |
||
741 | } else { |
||
742 | put_bits(&s->pb, 3, 1); /* motion only */ |
||
743 | if (!s->frame_pred_frame_dct) |
||
744 | put_bits(&s->pb, 2, 2); /* motion_type: frame */ |
||
745 | s->misc_bits += get_bits_diff(s); |
||
746 | // RAL: f_code parameter added |
||
747 | mpeg1_encode_motion(s, |
||
748 | motion_x - s->last_mv[0][0][0], |
||
749 | s->f_code); |
||
750 | // RAL: f_code parameter added |
||
751 | mpeg1_encode_motion(s, |
||
752 | motion_y - s->last_mv[0][0][1], |
||
753 | s->f_code); |
||
754 | s->qscale -= s->dquant; |
||
755 | s->mv_bits += get_bits_diff(s); |
||
756 | } |
||
757 | s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x; |
||
758 | s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y; |
||
759 | } else { |
||
760 | av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD); |
||
761 | |||
762 | if (cbp) { |
||
763 | if (s->dquant) { |
||
764 | put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */ |
||
765 | put_qscale(s); |
||
766 | } else { |
||
767 | put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */ |
||
768 | } |
||
769 | } else { |
||
770 | put_bits(&s->pb, 3, 1); /* motion only */ |
||
771 | put_bits(&s->pb, 2, 1); /* motion_type: field */ |
||
772 | s->qscale -= s->dquant; |
||
773 | } |
||
774 | s->misc_bits += get_bits_diff(s); |
||
775 | for (i = 0; i < 2; i++) { |
||
776 | put_bits(&s->pb, 1, s->field_select[0][i]); |
||
777 | mpeg1_encode_motion(s, |
||
778 | s->mv[0][i][0] - s->last_mv[0][i][0], |
||
779 | s->f_code); |
||
780 | mpeg1_encode_motion(s, |
||
781 | s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1), |
||
782 | s->f_code); |
||
783 | s->last_mv[0][i][0] = s->mv[0][i][0]; |
||
784 | s->last_mv[0][i][1] = 2 * s->mv[0][i][1]; |
||
785 | } |
||
786 | s->mv_bits += get_bits_diff(s); |
||
787 | } |
||
788 | if (cbp) { |
||
789 | if (s->chroma_y_shift) { |
||
790 | put_bits(&s->pb, |
||
791 | ff_mpeg12_mbPatTable[cbp][1], |
||
792 | ff_mpeg12_mbPatTable[cbp][0]); |
||
793 | } else { |
||
794 | put_bits(&s->pb, |
||
795 | ff_mpeg12_mbPatTable[cbp >> 2][1], |
||
796 | ff_mpeg12_mbPatTable[cbp >> 2][0]); |
||
797 | put_sbits(&s->pb, 2, cbp); |
||
798 | } |
||
799 | } |
||
800 | s->f_count++; |
||
801 | } else { |
||
802 | if (s->mv_type == MV_TYPE_16X16) { |
||
803 | if (cbp) { // With coded bloc pattern |
||
804 | if (s->dquant) { |
||
805 | if (s->mv_dir == MV_DIR_FORWARD) |
||
806 | put_mb_modes(s, 6, 3, 1, 0); |
||
807 | else |
||
808 | put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0); |
||
809 | put_qscale(s); |
||
810 | } else { |
||
811 | put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0); |
||
812 | } |
||
813 | } else { // No coded bloc pattern |
||
814 | put_bits(&s->pb, 5 - s->mv_dir, 2); |
||
815 | if (!s->frame_pred_frame_dct) |
||
816 | put_bits(&s->pb, 2, 2); /* motion_type: frame */ |
||
817 | s->qscale -= s->dquant; |
||
818 | } |
||
819 | s->misc_bits += get_bits_diff(s); |
||
820 | if (s->mv_dir & MV_DIR_FORWARD) { |
||
821 | mpeg1_encode_motion(s, |
||
822 | s->mv[0][0][0] - s->last_mv[0][0][0], |
||
823 | s->f_code); |
||
824 | mpeg1_encode_motion(s, |
||
825 | s->mv[0][0][1] - s->last_mv[0][0][1], |
||
826 | s->f_code); |
||
827 | s->last_mv[0][0][0] = |
||
828 | s->last_mv[0][1][0] = s->mv[0][0][0]; |
||
829 | s->last_mv[0][0][1] = |
||
830 | s->last_mv[0][1][1] = s->mv[0][0][1]; |
||
831 | s->f_count++; |
||
832 | } |
||
833 | if (s->mv_dir & MV_DIR_BACKWARD) { |
||
834 | mpeg1_encode_motion(s, |
||
835 | s->mv[1][0][0] - s->last_mv[1][0][0], |
||
836 | s->b_code); |
||
837 | mpeg1_encode_motion(s, |
||
838 | s->mv[1][0][1] - s->last_mv[1][0][1], |
||
839 | s->b_code); |
||
840 | s->last_mv[1][0][0] = |
||
841 | s->last_mv[1][1][0] = s->mv[1][0][0]; |
||
842 | s->last_mv[1][0][1] = |
||
843 | s->last_mv[1][1][1] = s->mv[1][0][1]; |
||
844 | s->b_count++; |
||
845 | } |
||
846 | } else { |
||
847 | av_assert2(s->mv_type == MV_TYPE_FIELD); |
||
848 | av_assert2(!s->frame_pred_frame_dct); |
||
849 | if (cbp) { // With coded bloc pattern |
||
850 | if (s->dquant) { |
||
851 | if (s->mv_dir == MV_DIR_FORWARD) |
||
852 | put_mb_modes(s, 6, 3, 1, 1); |
||
853 | else |
||
854 | put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1); |
||
855 | put_qscale(s); |
||
856 | } else { |
||
857 | put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1); |
||
858 | } |
||
859 | } else { // No coded bloc pattern |
||
860 | put_bits(&s->pb, 5 - s->mv_dir, 2); |
||
861 | put_bits(&s->pb, 2, 1); /* motion_type: field */ |
||
862 | s->qscale -= s->dquant; |
||
863 | } |
||
864 | s->misc_bits += get_bits_diff(s); |
||
865 | if (s->mv_dir & MV_DIR_FORWARD) { |
||
866 | for (i = 0; i < 2; i++) { |
||
867 | put_bits(&s->pb, 1, s->field_select[0][i]); |
||
868 | mpeg1_encode_motion(s, |
||
869 | s->mv[0][i][0] - s->last_mv[0][i][0], |
||
870 | s->f_code); |
||
871 | mpeg1_encode_motion(s, |
||
872 | s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1), |
||
873 | s->f_code); |
||
874 | s->last_mv[0][i][0] = s->mv[0][i][0]; |
||
875 | s->last_mv[0][i][1] = s->mv[0][i][1] * 2; |
||
876 | } |
||
877 | s->f_count++; |
||
878 | } |
||
879 | if (s->mv_dir & MV_DIR_BACKWARD) { |
||
880 | for (i = 0; i < 2; i++) { |
||
881 | put_bits(&s->pb, 1, s->field_select[1][i]); |
||
882 | mpeg1_encode_motion(s, |
||
883 | s->mv[1][i][0] - s->last_mv[1][i][0], |
||
884 | s->b_code); |
||
885 | mpeg1_encode_motion(s, |
||
886 | s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1), |
||
887 | s->b_code); |
||
888 | s->last_mv[1][i][0] = s->mv[1][i][0]; |
||
889 | s->last_mv[1][i][1] = s->mv[1][i][1] * 2; |
||
890 | } |
||
891 | s->b_count++; |
||
892 | } |
||
893 | } |
||
894 | s->mv_bits += get_bits_diff(s); |
||
895 | if (cbp) { |
||
896 | if (s->chroma_y_shift) { |
||
897 | put_bits(&s->pb, |
||
898 | ff_mpeg12_mbPatTable[cbp][1], |
||
899 | ff_mpeg12_mbPatTable[cbp][0]); |
||
900 | } else { |
||
901 | put_bits(&s->pb, |
||
902 | ff_mpeg12_mbPatTable[cbp >> 2][1], |
||
903 | ff_mpeg12_mbPatTable[cbp >> 2][0]); |
||
904 | put_sbits(&s->pb, 2, cbp); |
||
905 | } |
||
906 | } |
||
907 | } |
||
908 | for (i = 0; i < mb_block_count; i++) |
||
909 | if (cbp & (1 << (mb_block_count - 1 - i))) |
||
910 | mpeg1_encode_block(s, block[i], i); |
||
911 | s->mb_skip_run = 0; |
||
912 | if (s->mb_intra) |
||
913 | s->i_tex_bits += get_bits_diff(s); |
||
914 | else |
||
915 | s->p_tex_bits += get_bits_diff(s); |
||
916 | } |
||
917 | } |
||
918 | |||
919 | void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64], |
||
920 | int motion_x, int motion_y) |
||
921 | { |
||
922 | if (s->chroma_format == CHROMA_420) |
||
923 | mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6); |
||
924 | else |
||
925 | mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8); |
||
926 | } |
||
927 | |||
928 | av_cold void ff_mpeg1_encode_init(MpegEncContext *s) |
||
929 | { |
||
930 | static int done = 0; |
||
931 | |||
932 | ff_mpeg12_common_init(s); |
||
933 | |||
934 | if (!done) { |
||
935 | int f_code; |
||
936 | int mv; |
||
937 | int i; |
||
938 | |||
939 | done = 1; |
||
940 | ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]); |
||
941 | ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]); |
||
942 | |||
943 | for (i = 0; i < 64; i++) { |
||
944 | mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i]; |
||
945 | mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i]; |
||
946 | } |
||
947 | |||
948 | init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len); |
||
949 | if (s->intra_vlc_format) |
||
950 | init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len); |
||
951 | |||
952 | /* build unified dc encoding tables */ |
||
953 | for (i = -255; i < 256; i++) { |
||
954 | int adiff, index; |
||
955 | int bits, code; |
||
956 | int diff = i; |
||
957 | |||
958 | adiff = FFABS(diff); |
||
959 | if (diff < 0) |
||
960 | diff--; |
||
961 | index = av_log2(2 * adiff); |
||
962 | |||
963 | bits = ff_mpeg12_vlc_dc_lum_bits[index] + index; |
||
964 | code = (ff_mpeg12_vlc_dc_lum_code[index] << index) + |
||
965 | (diff & ((1 << index) - 1)); |
||
966 | mpeg1_lum_dc_uni[i + 255] = bits + (code << 8); |
||
967 | |||
968 | bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index; |
||
969 | code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) + |
||
970 | (diff & ((1 << index) - 1)); |
||
971 | mpeg1_chr_dc_uni[i + 255] = bits + (code << 8); |
||
972 | } |
||
973 | |||
974 | for (f_code = 1; f_code <= MAX_FCODE; f_code++) |
||
975 | for (mv = -MAX_MV; mv <= MAX_MV; mv++) { |
||
976 | int len; |
||
977 | |||
978 | if (mv == 0) { |
||
979 | len = ff_mpeg12_mbMotionVectorTable[0][1]; |
||
980 | } else { |
||
981 | int val, bit_size, code; |
||
982 | |||
983 | bit_size = f_code - 1; |
||
984 | |||
985 | val = mv; |
||
986 | if (val < 0) |
||
987 | val = -val; |
||
988 | val--; |
||
989 | code = (val >> bit_size) + 1; |
||
990 | if (code < 17) |
||
991 | len = ff_mpeg12_mbMotionVectorTable[code][1] + |
||
992 | 1 + bit_size; |
||
993 | else |
||
994 | len = ff_mpeg12_mbMotionVectorTable[16][1] + |
||
995 | 2 + bit_size; |
||
996 | } |
||
997 | |||
998 | mv_penalty[f_code][mv + MAX_MV] = len; |
||
999 | } |
||
1000 | |||
1001 | |||
1002 | for (f_code = MAX_FCODE; f_code > 0; f_code--) |
||
1003 | for (mv = -(8 << f_code); mv < (8 << f_code); mv++) |
||
1004 | fcode_tab[mv + MAX_MV] = f_code; |
||
1005 | } |
||
1006 | s->me.mv_penalty = mv_penalty; |
||
1007 | s->fcode_tab = fcode_tab; |
||
1008 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) { |
||
1009 | s->min_qcoeff = -255; |
||
1010 | s->max_qcoeff = 255; |
||
1011 | } else { |
||
1012 | s->min_qcoeff = -2047; |
||
1013 | s->max_qcoeff = 2047; |
||
1014 | } |
||
1015 | if (s->intra_vlc_format) { |
||
1016 | s->intra_ac_vlc_length = |
||
1017 | s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len; |
||
1018 | } else { |
||
1019 | s->intra_ac_vlc_length = |
||
1020 | s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len; |
||
1021 | } |
||
1022 | s->inter_ac_vlc_length = |
||
1023 | s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len; |
||
1024 | } |
||
1025 | |||
1026 | #define OFFSET(x) offsetof(MpegEncContext, x) |
||
1027 | #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM |
||
1028 | #define COMMON_OPTS \ |
||
1029 | { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", \ |
||
1030 | OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\ |
||
1031 | { "intra_vlc", "Use MPEG-2 intra VLC table.", \ |
||
1032 | OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \ |
||
1033 | { "drop_frame_timecode", "Timecode is in drop frame format.", \ |
||
1034 | OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \ |
||
1035 | { "scan_offset", "Reserve space for SVCD scan offset user data.", \ |
||
1036 | OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, |
||
1037 | |||
1038 | static const AVOption mpeg1_options[] = { |
||
1039 | COMMON_OPTS |
||
1040 | FF_MPV_COMMON_OPTS |
||
1041 | { NULL }, |
||
1042 | }; |
||
1043 | |||
1044 | static const AVOption mpeg2_options[] = { |
||
1045 | COMMON_OPTS |
||
1046 | { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, |
||
1047 | { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, |
||
1048 | FF_MPV_COMMON_OPTS |
||
1049 | { NULL }, |
||
1050 | }; |
||
1051 | |||
1052 | #define mpeg12_class(x) \ |
||
1053 | static const AVClass mpeg ## x ## _class = { \ |
||
1054 | .class_name = "mpeg" # x "video encoder", \ |
||
1055 | .item_name = av_default_item_name, \ |
||
1056 | .option = mpeg ## x ## _options, \ |
||
1057 | .version = LIBAVUTIL_VERSION_INT, \ |
||
1058 | }; |
||
1059 | |||
1060 | mpeg12_class(1) |
||
1061 | mpeg12_class(2) |
||
1062 | |||
1063 | AVCodec ff_mpeg1video_encoder = { |
||
1064 | .name = "mpeg1video", |
||
1065 | .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"), |
||
1066 | .type = AVMEDIA_TYPE_VIDEO, |
||
1067 | .id = AV_CODEC_ID_MPEG1VIDEO, |
||
1068 | .priv_data_size = sizeof(MpegEncContext), |
||
1069 | .init = encode_init, |
||
1070 | .encode2 = ff_MPV_encode_picture, |
||
1071 | .close = ff_MPV_encode_end, |
||
1072 | .supported_framerates = ff_mpeg12_frame_rate_tab + 1, |
||
1073 | .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, |
||
1074 | AV_PIX_FMT_NONE }, |
||
1075 | .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS, |
||
1076 | .priv_class = &mpeg1_class, |
||
1077 | }; |
||
1078 | |||
1079 | AVCodec ff_mpeg2video_encoder = { |
||
1080 | .name = "mpeg2video", |
||
1081 | .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"), |
||
1082 | .type = AVMEDIA_TYPE_VIDEO, |
||
1083 | .id = AV_CODEC_ID_MPEG2VIDEO, |
||
1084 | .priv_data_size = sizeof(MpegEncContext), |
||
1085 | .init = encode_init, |
||
1086 | .encode2 = ff_MPV_encode_picture, |
||
1087 | .close = ff_MPV_encode_end, |
||
1088 | .supported_framerates = ff_mpeg2_frame_rate_tab, |
||
1089 | .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, |
||
1090 | AV_PIX_FMT_YUV422P, |
||
1091 | AV_PIX_FMT_NONE }, |
||
1092 | .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS, |
||
1093 | .priv_class = &mpeg2_class, |
||
1094 | };><>>><>>>=>=>><>><>><>><>><>><>>>>><>>>>>><>>>>5%>><>=>=>=>><>><>><>><>>=>><>>=>=>=>>=>=>=>=>=>>=>>>=>=>=>=>=>>>>=>=>>>>>> |