Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * MPEG-1/2 encoder | ||
3 | * Copyright (c) 2000,2001 Fabrice Bellard | ||
4 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | ||
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 | * MPEG-1/2 encoder | ||
26 | */ | ||
27 | |||
28 | #include <stdint.h> | ||
29 | |||
30 | #include "config.h" | ||
31 | #include "config_components.h" | ||
32 | #include "libavutil/attributes.h" | ||
33 | #include "libavutil/avassert.h" | ||
34 | #include "libavutil/log.h" | ||
35 | #include "libavutil/opt.h" | ||
36 | #include "libavutil/thread.h" | ||
37 | #include "libavutil/timecode.h" | ||
38 | #include "libavutil/stereo3d.h" | ||
39 | |||
40 | #include "avcodec.h" | ||
41 | #include "codec_internal.h" | ||
42 | #include "mathops.h" | ||
43 | #include "mpeg12.h" | ||
44 | #include "mpeg12data.h" | ||
45 | #include "mpeg12enc.h" | ||
46 | #include "mpeg12vlc.h" | ||
47 | #include "mpegutils.h" | ||
48 | #include "mpegvideo.h" | ||
49 | #include "mpegvideodata.h" | ||
50 | #include "mpegvideoenc.h" | ||
51 | #include "profiles.h" | ||
52 | #include "rl.h" | ||
53 | |||
54 | #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER | ||
55 | static const uint8_t svcd_scan_offset_placeholder[] = { | ||
56 | 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80, | ||
57 | 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | ||
58 | }; | ||
59 | |||
60 | static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1]; | ||
61 | static uint8_t fcode_tab[MAX_MV * 2 + 1]; | ||
62 | |||
63 | static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2]; | ||
64 | static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2]; | ||
65 | |||
66 | static uint8_t mpeg12_max_level[MAX_LEVEL + 1]; | ||
67 | static uint8_t mpeg12_index_run[MAX_RUN + 1]; | ||
68 | |||
69 | /* simple include everything table for dc, first byte is bits | ||
70 | * number next 3 are code */ | ||
71 | static uint32_t mpeg1_lum_dc_uni[512]; | ||
72 | static uint32_t mpeg1_chr_dc_uni[512]; | ||
73 | |||
74 | typedef struct MPEG12EncContext { | ||
75 | MpegEncContext mpeg; | ||
76 | AVRational frame_rate_ext; | ||
77 | unsigned frame_rate_index; | ||
78 | |||
79 | int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num | ||
80 | |||
81 | int64_t timecode_frame_start; ///< GOP timecode frame start number, in non drop frame format | ||
82 | AVTimecode tc; ///< timecode context | ||
83 | char *tc_opt_str; ///< timecode option string | ||
84 | int drop_frame_timecode; ///< timecode is in drop frame format. | ||
85 | int scan_offset; ///< reserve space for SVCD scan offset user data. | ||
86 | |||
87 | int a53_cc; | ||
88 | int seq_disp_ext; | ||
89 | int video_format; | ||
90 | #define VIDEO_FORMAT_COMPONENT 0 | ||
91 | #define VIDEO_FORMAT_PAL 1 | ||
92 | #define VIDEO_FORMAT_NTSC 2 | ||
93 | #define VIDEO_FORMAT_SECAM 3 | ||
94 | #define VIDEO_FORMAT_MAC 4 | ||
95 | #define VIDEO_FORMAT_UNSPECIFIED 5 | ||
96 | } MPEG12EncContext; | ||
97 | |||
98 | #define A53_MAX_CC_COUNT 0x1f | ||
99 | #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */ | ||
100 | |||
101 | 117 | av_cold void ff_mpeg1_init_uni_ac_vlc(const int8_t max_level[], | |
102 | const uint8_t index_run[], | ||
103 | const uint16_t table_vlc[][2], | ||
104 | uint8_t uni_ac_vlc_len[]) | ||
105 | { | ||
106 | int i; | ||
107 | |||
108 |
2/2✓ Branch 0 taken 14976 times.
✓ Branch 1 taken 117 times.
|
15093 | for (i = 0; i < 128; i++) { |
109 | 14976 | int level = i - 64; | |
110 | int run; | ||
111 |
2/2✓ Branch 0 taken 117 times.
✓ Branch 1 taken 14859 times.
|
14976 | if (!level) |
112 | 117 | continue; | |
113 |
2/2✓ Branch 0 taken 950976 times.
✓ Branch 1 taken 14859 times.
|
965835 | for (run = 0; run < 64; run++) { |
114 | int len, code; | ||
115 | 950976 | int alevel = FFABS(level); | |
116 | |||
117 |
2/2✓ Branch 0 taken 924822 times.
✓ Branch 1 taken 26154 times.
|
950976 | if (alevel > max_level[run]) |
118 | 924822 | code = 111; /* rl->n */ | |
119 | else | ||
120 | 26154 | code = index_run[run] + alevel - 1; | |
121 | |||
122 |
2/2✓ Branch 0 taken 25974 times.
✓ Branch 1 taken 925002 times.
|
950976 | if (code < 111) { /* rl->n */ |
123 | /* length of VLC and sign */ | ||
124 | 25974 | len = table_vlc[code][1] + 1; | |
125 | } else { | ||
126 | 925002 | len = table_vlc[MPEG12_RL_NB_ELEMS][1] + 6; | |
127 | |||
128 |
1/2✓ Branch 0 taken 925002 times.
✗ Branch 1 not taken.
|
925002 | if (alevel < 128) |
129 | 925002 | len += 8; | |
130 | else | ||
131 | ✗ | len += 16; | |
132 | } | ||
133 | |||
134 | 950976 | uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len; | |
135 | } | ||
136 | } | ||
137 | 117 | } | |
138 | |||
139 | #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER | ||
140 | 54 | static int find_frame_rate_index(MPEG12EncContext *mpeg12) | |
141 | { | ||
142 | 54 | MpegEncContext *const s = &mpeg12->mpeg; | |
143 | int i; | ||
144 | 54 | AVRational bestq = (AVRational) {0, 0}; | |
145 | AVRational ext; | ||
146 | 54 | AVRational target = av_inv_q(s->avctx->time_base); | |
147 | |||
148 |
2/2✓ Branch 0 taken 490 times.
✓ Branch 1 taken 1 times.
|
491 | for (i = 1; i < 14; i++) { |
149 |
4/4✓ Branch 0 taken 477 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 424 times.
|
490 | if (s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL && |
150 | i >= 9) | ||
151 | 53 | break; | |
152 | |||
153 |
2/2✓ Branch 0 taken 1748 times.
✓ Branch 1 taken 437 times.
|
2185 | for (ext.num=1; ext.num <= 4; ext.num++) { |
154 |
2/2✓ Branch 0 taken 55936 times.
✓ Branch 1 taken 1748 times.
|
57684 | for (ext.den=1; ext.den <= 32; ext.den++) { |
155 | 55936 | AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]); | |
156 | |||
157 |
6/6✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 44672 times.
✓ Branch 2 taken 352 times.
✓ Branch 3 taken 10912 times.
✓ Branch 4 taken 264 times.
✓ Branch 5 taken 88 times.
|
55936 | if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1)) |
158 | 25834 | continue; | |
159 |
2/2✓ Branch 0 taken 14658 times.
✓ Branch 1 taken 30102 times.
|
44760 | if (av_gcd(ext.den, ext.num) != 1) |
160 | 14658 | continue; | |
161 | |||
162 |
2/2✓ Branch 0 taken 30048 times.
✓ Branch 1 taken 54 times.
|
30102 | if ( bestq.num==0 |
163 |
2/2✓ Branch 1 taken 29912 times.
✓ Branch 2 taken 136 times.
|
30048 | || av_nearer_q(target, bestq, q) < 0 |
164 |
5/6✓ Branch 0 taken 11076 times.
✓ Branch 1 taken 18836 times.
✓ Branch 2 taken 281 times.
✓ Branch 3 taken 10795 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 281 times.
|
29912 | || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) { |
165 | 190 | bestq = q; | |
166 | 190 | mpeg12->frame_rate_index = i; | |
167 | 190 | mpeg12->frame_rate_ext.num = ext.num; | |
168 | 190 | mpeg12->frame_rate_ext.den = ext.den; | |
169 | } | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | |||
174 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 53 times.
|
54 | if (av_cmp_q(target, bestq)) |
175 | 1 | return -1; | |
176 | else | ||
177 | 53 | return 0; | |
178 | } | ||
179 | |||
180 | 54 | static av_cold int encode_init(AVCodecContext *avctx) | |
181 | { | ||
182 | 54 | MPEG12EncContext *const mpeg12 = avctx->priv_data; | |
183 | 54 | MpegEncContext *const s = &mpeg12->mpeg; | |
184 | int ret; | ||
185 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 11 times.
|
54 | int max_size = avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 16383 : 4095; |
186 | |||
187 |
2/4✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
|
54 | if (avctx->width > max_size || avctx->height > max_size) { |
188 | ✗ | av_log(avctx, AV_LOG_ERROR, "%s does not support resolutions above %dx%d\n", | |
189 | ✗ | CONFIG_SMALL ? avctx->codec->name : avctx->codec->long_name, | |
190 | max_size, max_size); | ||
191 | ✗ | return AVERROR(EINVAL); | |
192 | } | ||
193 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) { |
194 | ✗ | av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n"); | |
195 | ✗ | return AVERROR(EINVAL); | |
196 | } | ||
197 | |||
198 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 1 times.
|
54 | if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { |
199 |
2/4✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 53 times.
|
53 | if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) { |
200 | ✗ | av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiples of 4096\n" | |
201 | "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL); | ||
202 | ✗ | return AVERROR(EINVAL); | |
203 | } | ||
204 | } | ||
205 | |||
206 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | if (avctx->profile == AV_PROFILE_UNKNOWN) { |
207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (avctx->level != AV_LEVEL_UNKNOWN) { |
208 | ✗ | av_log(avctx, AV_LOG_ERROR, "Set profile and level\n"); | |
209 | ✗ | return AVERROR(EINVAL); | |
210 | } | ||
211 | /* Main or 4:2:2 */ | ||
212 | 54 | avctx->profile = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? AV_PROFILE_MPEG2_MAIN | |
213 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 5 times.
|
54 | : AV_PROFILE_MPEG2_422; |
214 | } | ||
215 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | if (avctx->level == AV_LEVEL_UNKNOWN) { |
216 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 49 times.
|
54 | if (avctx->profile == AV_PROFILE_MPEG2_422) { /* 4:2:2 */ |
217 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if (avctx->width <= 720 && avctx->height <= 608) |
218 | 5 | avctx->level = 5; /* Main */ | |
219 | else | ||
220 | ✗ | avctx->level = 2; /* High */ | |
221 | } else { | ||
222 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (avctx->profile != AV_PROFILE_MPEG2_HIGH && |
223 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | avctx->pix_fmt != AV_PIX_FMT_YUV420P) { |
224 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
225 | "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n"); | ||
226 | ✗ | return AVERROR(EINVAL); | |
227 | } | ||
228 |
3/4✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
49 | if (avctx->width <= 720 && avctx->height <= 576) |
229 | 48 | avctx->level = 8; /* Main */ | |
230 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | else if (avctx->width <= 1440) |
231 | 1 | avctx->level = 6; /* High 1440 */ | |
232 | else | ||
233 | ✗ | avctx->level = 4; /* High */ | |
234 | } | ||
235 | } | ||
236 | |||
237 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
|
54 | if ((ret = ff_mpv_encode_init(avctx)) < 0) |
238 | ✗ | return ret; | |
239 | |||
240 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 53 times.
|
54 | if (find_frame_rate_index(mpeg12) < 0) { |
241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { |
242 | ✗ | av_log(avctx, AV_LOG_ERROR, "MPEG-1/2 does not support %d/%d fps\n", | |
243 | avctx->time_base.den, avctx->time_base.num); | ||
244 | ✗ | return AVERROR(EINVAL); | |
245 | } else { | ||
246 | 1 | av_log(avctx, AV_LOG_INFO, | |
247 | "MPEG-1/2 does not support %d/%d fps, there may be AV sync issues\n", | ||
248 | avctx->time_base.den, avctx->time_base.num); | ||
249 | } | ||
250 | } | ||
251 | |||
252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (mpeg12->drop_frame_timecode) |
253 | ✗ | mpeg12->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME; | |
254 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
54 | if (mpeg12->drop_frame_timecode && mpeg12->frame_rate_index != 4) { |
255 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
256 | "Drop frame time code only allowed with 1001/30000 fps\n"); | ||
257 | ✗ | return AVERROR(EINVAL); | |
258 | } | ||
259 | |||
260 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 48 times.
|
54 | if (mpeg12->tc_opt_str) { |
261 | 6 | AVRational rate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index]; | |
262 | 6 | int ret = av_timecode_init_from_string(&mpeg12->tc, rate, mpeg12->tc_opt_str, s); | |
263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
264 | ✗ | return ret; | |
265 | 6 | mpeg12->drop_frame_timecode = !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME); | |
266 | 6 | mpeg12->timecode_frame_start = mpeg12->tc.start; | |
267 | } else { | ||
268 | 48 | mpeg12->timecode_frame_start = 0; // default is -1 | |
269 | } | ||
270 | |||
271 | 54 | return 0; | |
272 | } | ||
273 | |||
274 | 306201 | static void put_header(MpegEncContext *s, int header) | |
275 | { | ||
276 | 306201 | align_put_bits(&s->pb); | |
277 | 306201 | put_bits(&s->pb, 16, header >> 16); | |
278 | 306201 | put_sbits(&s->pb, 16, header); | |
279 | 306201 | } | |
280 | |||
281 | /* put sequence header if needed */ | ||
282 | 3526 | static void mpeg1_encode_sequence_header(MpegEncContext *s) | |
283 | { | ||
284 | 3526 | MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s; | |
285 | unsigned int vbv_buffer_size, fps, v; | ||
286 | int constraint_parameter_flag; | ||
287 | 3526 | AVRational framerate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index]; | |
288 | uint64_t time_code; | ||
289 | 3526 | int64_t best_aspect_error = INT64_MAX; | |
290 | 3526 | AVRational aspect_ratio = s->avctx->sample_aspect_ratio; | |
291 | int aspect_ratio_info; | ||
292 | |||
293 |
2/2✓ Branch 0 taken 3117 times.
✓ Branch 1 taken 409 times.
|
3526 | if (!(s->current_picture.f->flags & AV_FRAME_FLAG_KEY)) |
294 | 3117 | return; | |
295 | |||
296 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 401 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
409 | if (aspect_ratio.num == 0 || aspect_ratio.den == 0) |
297 | 401 | aspect_ratio = (AVRational){1,1}; // pixel aspect 1.1 (VGA) | |
298 | |||
299 | /* MPEG-1 header repeated every GOP */ | ||
300 | 409 | put_header(s, SEQ_START_CODE); | |
301 | |||
302 | 409 | put_sbits(&s->pb, 12, s->width & 0xFFF); | |
303 | 409 | put_sbits(&s->pb, 12, s->height & 0xFFF); | |
304 | |||
305 |
2/2✓ Branch 0 taken 5726 times.
✓ Branch 1 taken 409 times.
|
6135 | for (int i = 1; i < 15; i++) { |
306 | 5726 | int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den; | |
307 |
4/4✓ Branch 0 taken 4984 times.
✓ Branch 1 taken 742 times.
✓ Branch 2 taken 356 times.
✓ Branch 3 taken 4628 times.
|
5726 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1) |
308 | 1098 | error -= (1LL<<32) / ff_mpeg1_aspect[i]; | |
309 | else | ||
310 | 4628 | error -= (1LL<<32)*ff_mpeg2_aspect[i].num * s->height / s->width / ff_mpeg2_aspect[i].den; | |
311 | |||
312 | 5726 | error = FFABS(error); | |
313 | |||
314 |
2/2✓ Branch 0 taken 412 times.
✓ Branch 1 taken 5314 times.
|
5726 | if (error - 2 <= best_aspect_error) { |
315 | 412 | best_aspect_error = error; | |
316 | 412 | aspect_ratio_info = i; | |
317 | } | ||
318 | } | ||
319 | |||
320 | 409 | put_bits(&s->pb, 4, aspect_ratio_info); | |
321 | 409 | put_bits(&s->pb, 4, mpeg12->frame_rate_index); | |
322 | |||
323 |
2/2✓ Branch 0 taken 153 times.
✓ Branch 1 taken 256 times.
|
409 | if (s->avctx->rc_max_rate) { |
324 | 153 | v = (s->avctx->rc_max_rate + 399) / 400; | |
325 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
153 | if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
326 | ✗ | v = 0x3ffff; | |
327 | } else { | ||
328 | 256 | v = 0x3FFFF; | |
329 | } | ||
330 | |||
331 |
2/2✓ Branch 0 taken 153 times.
✓ Branch 1 taken 256 times.
|
409 | if (s->avctx->rc_buffer_size) |
332 | 153 | vbv_buffer_size = s->avctx->rc_buffer_size; | |
333 | else | ||
334 | /* VBV calculation: Scaled so that a VCD has the proper | ||
335 | * VBV size of 40 kilobytes */ | ||
336 | 256 | vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024; | |
337 | 409 | vbv_buffer_size = (vbv_buffer_size + 16383) / 16384; | |
338 | |||
339 | 409 | put_sbits(&s->pb, 18, v); | |
340 | 409 | put_bits(&s->pb, 1, 1); // marker | |
341 | 409 | put_sbits(&s->pb, 10, vbv_buffer_size); | |
342 | |||
343 | 409 | constraint_parameter_flag = | |
344 | 815 | s->width <= 768 && | |
345 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 150 times.
|
406 | s->height <= 576 && |
346 |
2/2✓ Branch 0 taken 233 times.
✓ Branch 1 taken 23 times.
|
256 | s->mb_width * s->mb_height <= 396 && |
347 |
2/2✓ Branch 0 taken 227 times.
✓ Branch 1 taken 6 times.
|
233 | s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den && |
348 |
1/2✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
|
227 | framerate.num <= framerate.den * 30 && |
349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 227 times.
|
227 | s->avctx->me_range && |
350 | ✗ | s->avctx->me_range < 128 && | |
351 | ✗ | vbv_buffer_size <= 20 && | |
352 |
2/2✓ Branch 0 taken 406 times.
✓ Branch 1 taken 3 times.
|
815 | v <= 1856000 / 400 && |
353 | ✗ | s->codec_id == AV_CODEC_ID_MPEG1VIDEO; | |
354 | |||
355 | 409 | put_bits(&s->pb, 1, constraint_parameter_flag); | |
356 | |||
357 | 409 | ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix); | |
358 | 409 | ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix); | |
359 | |||
360 |
2/2✓ Branch 0 taken 356 times.
✓ Branch 1 taken 53 times.
|
409 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
361 | const AVFrameSideData *side_data; | ||
362 | 356 | int width = s->width; | |
363 | 356 | int height = s->height; | |
364 | int use_seq_disp_ext; | ||
365 | |||
366 | 356 | put_header(s, EXT_START_CODE); | |
367 | 356 | put_bits(&s->pb, 4, 1); // seq ext | |
368 | |||
369 | 356 | put_bits(&s->pb, 1, s->avctx->profile == AV_PROFILE_MPEG2_422); // escx 1 for 4:2:2 profile | |
370 | |||
371 | 356 | put_bits(&s->pb, 3, s->avctx->profile); // profile | |
372 | 356 | put_bits(&s->pb, 4, s->avctx->level); // level | |
373 | |||
374 | 356 | put_bits(&s->pb, 1, s->progressive_sequence); | |
375 | 356 | put_bits(&s->pb, 2, s->chroma_format); | |
376 | 356 | put_bits(&s->pb, 2, s->width >> 12); | |
377 | 356 | put_bits(&s->pb, 2, s->height >> 12); | |
378 | 356 | put_bits(&s->pb, 12, v >> 18); // bitrate ext | |
379 | 356 | put_bits(&s->pb, 1, 1); // marker | |
380 | 356 | put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext | |
381 | 356 | put_bits(&s->pb, 1, s->low_delay); | |
382 | 356 | put_bits(&s->pb, 2, mpeg12->frame_rate_ext.num-1); // frame_rate_ext_n | |
383 | 356 | put_bits(&s->pb, 5, mpeg12->frame_rate_ext.den-1); // frame_rate_ext_d | |
384 | |||
385 | 356 | side_data = av_frame_get_side_data(s->current_picture_ptr->f, AV_FRAME_DATA_PANSCAN); | |
386 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 351 times.
|
356 | if (side_data) { |
387 | 5 | const AVPanScan *pan_scan = (AVPanScan *)side_data->data; | |
388 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if (pan_scan->width && pan_scan->height) { |
389 | 5 | width = pan_scan->width >> 4; | |
390 | 5 | height = pan_scan->height >> 4; | |
391 | } | ||
392 | } | ||
393 | |||
394 | 1068 | use_seq_disp_ext = (width != s->width || | |
395 |
1/2✓ Branch 0 taken 356 times.
✗ Branch 1 not taken.
|
356 | height != s->height || |
396 |
2/2✓ Branch 0 taken 347 times.
✓ Branch 1 taken 9 times.
|
356 | s->avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || |
397 |
2/2✓ Branch 0 taken 344 times.
✓ Branch 1 taken 3 times.
|
347 | s->avctx->color_trc != AVCOL_TRC_UNSPECIFIED || |
398 |
2/4✓ Branch 0 taken 356 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 344 times.
✗ Branch 3 not taken.
|
1056 | s->avctx->colorspace != AVCOL_SPC_UNSPECIFIED || |
399 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 344 times.
|
344 | mpeg12->video_format != VIDEO_FORMAT_UNSPECIFIED); |
400 | |||
401 |
1/2✓ Branch 0 taken 356 times.
✗ Branch 1 not taken.
|
356 | if (mpeg12->seq_disp_ext == 1 || |
402 |
3/4✓ Branch 0 taken 356 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 344 times.
|
356 | (mpeg12->seq_disp_ext == -1 && use_seq_disp_ext)) { |
403 | 12 | put_header(s, EXT_START_CODE); | |
404 | 12 | put_bits(&s->pb, 4, 2); // sequence display extension | |
405 | 12 | put_bits(&s->pb, 3, mpeg12->video_format); // video_format | |
406 | 12 | put_bits(&s->pb, 1, 1); // colour_description | |
407 | 12 | put_bits(&s->pb, 8, s->avctx->color_primaries); // colour_primaries | |
408 | 12 | put_bits(&s->pb, 8, s->avctx->color_trc); // transfer_characteristics | |
409 | 12 | put_bits(&s->pb, 8, s->avctx->colorspace); // matrix_coefficients | |
410 | 12 | put_bits(&s->pb, 14, width); // display_horizontal_size | |
411 | 12 | put_bits(&s->pb, 1, 1); // marker_bit | |
412 | 12 | put_bits(&s->pb, 14, height); // display_vertical_size | |
413 | 12 | put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding | |
414 | } | ||
415 | } | ||
416 | |||
417 | 409 | put_header(s, GOP_START_CODE); | |
418 | 409 | put_bits(&s->pb, 1, mpeg12->drop_frame_timecode); // drop frame flag | |
419 | /* time code: we must convert from the real frame rate to a | ||
420 | * fake MPEG frame rate in case of low frame rate */ | ||
421 | 409 | fps = (framerate.num + framerate.den / 2) / framerate.den; | |
422 | 409 | time_code = s->current_picture_ptr->coded_picture_number + | |
423 | 409 | mpeg12->timecode_frame_start; | |
424 | |||
425 | 409 | mpeg12->gop_picture_number = s->current_picture_ptr->coded_picture_number; | |
426 | |||
427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 409 times.
|
409 | av_assert0(mpeg12->drop_frame_timecode == !!(mpeg12->tc.flags & AV_TIMECODE_FLAG_DROPFRAME)); |
428 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 400 times.
|
409 | if (mpeg12->drop_frame_timecode) |
429 | 9 | time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps); | |
430 | |||
431 | 409 | put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); | |
432 | 409 | put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); | |
433 | 409 | put_bits(&s->pb, 1, 1); | |
434 | 409 | put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60)); | |
435 | 409 | put_bits(&s->pb, 6, (uint32_t)((time_code % fps))); | |
436 | 409 | put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) || | |
437 |
5/6✓ Branch 0 taken 409 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✓ Branch 3 taken 150 times.
✓ Branch 4 taken 53 times.
✓ Branch 5 taken 206 times.
|
409 | s->intra_only || !mpeg12->gop_picture_number); |
438 | 409 | put_bits(&s->pb, 1, 0); // broken link | |
439 | } | ||
440 | |||
441 | 1757849 | static inline void encode_mb_skip_run(MpegEncContext *s, int run) | |
442 | { | ||
443 |
2/2✓ Branch 0 taken 69199 times.
✓ Branch 1 taken 1757849 times.
|
1827048 | while (run >= 33) { |
444 | 69199 | put_bits(&s->pb, 11, 0x008); | |
445 | 69199 | run -= 33; | |
446 | } | ||
447 | 1757849 | put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1], | |
448 | 1757849 | ff_mpeg12_mbAddrIncrTable[run][0]); | |
449 | 1757849 | } | |
450 | |||
451 | 480466 | static av_always_inline void put_qscale(MpegEncContext *s) | |
452 | { | ||
453 | 480466 | put_bits(&s->pb, 5, s->qscale); | |
454 | 480466 | } | |
455 | |||
456 | 298309 | void ff_mpeg1_encode_slice_header(MpegEncContext *s) | |
457 | { | ||
458 |
3/4✓ Branch 0 taken 291036 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 291036 times.
|
298309 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) { |
459 | ✗ | put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127)); | |
460 | /* slice_vertical_position_extension */ | ||
461 | ✗ | put_bits(&s->pb, 3, s->mb_y >> 7); | |
462 | } else { | ||
463 | 298309 | put_header(s, SLICE_MIN_START_CODE + s->mb_y); | |
464 | } | ||
465 | 298309 | put_qscale(s); | |
466 | /* slice extra information */ | ||
467 | 298309 | put_bits(&s->pb, 1, 0); | |
468 | 298309 | } | |
469 | |||
470 | 3526 | void ff_mpeg1_encode_picture_header(MpegEncContext *s) | |
471 | { | ||
472 | 3526 | MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s; | |
473 | AVFrameSideData *side_data; | ||
474 | 3526 | mpeg1_encode_sequence_header(s); | |
475 | |||
476 | /* MPEG-1 picture header */ | ||
477 | 3526 | put_header(s, PICTURE_START_CODE); | |
478 | /* temporal reference */ | ||
479 | |||
480 | // RAL: s->picture_number instead of s->fake_picture_number | ||
481 | 3526 | put_bits(&s->pb, 10, | |
482 | 3526 | (s->picture_number - mpeg12->gop_picture_number) & 0x3ff); | |
483 | 3526 | put_bits(&s->pb, 3, s->pict_type); | |
484 | |||
485 | 3526 | s->vbv_delay_pos = put_bytes_count(&s->pb, 0); | |
486 | 3526 | put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */ | |
487 | |||
488 | // RAL: Forward f_code also needed for B-frames | ||
489 |
2/2✓ Branch 0 taken 1116 times.
✓ Branch 1 taken 2410 times.
|
3526 | if (s->pict_type == AV_PICTURE_TYPE_P || |
490 |
2/2✓ Branch 0 taken 707 times.
✓ Branch 1 taken 409 times.
|
1116 | s->pict_type == AV_PICTURE_TYPE_B) { |
491 | 3117 | put_bits(&s->pb, 1, 0); /* half pel coordinates */ | |
492 |
2/2✓ Branch 0 taken 427 times.
✓ Branch 1 taken 2690 times.
|
3117 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
493 | 427 | put_bits(&s->pb, 3, s->f_code); /* forward_f_code */ | |
494 | else | ||
495 | 2690 | put_bits(&s->pb, 3, 7); /* forward_f_code */ | |
496 | } | ||
497 | |||
498 | // RAL: Backward f_code necessary for B-frames | ||
499 |
2/2✓ Branch 0 taken 707 times.
✓ Branch 1 taken 2819 times.
|
3526 | if (s->pict_type == AV_PICTURE_TYPE_B) { |
500 | 707 | put_bits(&s->pb, 1, 0); /* half pel coordinates */ | |
501 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 563 times.
|
707 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) |
502 | 144 | put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ | |
503 | else | ||
504 | 563 | put_bits(&s->pb, 3, 7); /* backward_f_code */ | |
505 | } | ||
506 | |||
507 | 3526 | put_bits(&s->pb, 1, 0); /* extra bit picture */ | |
508 | |||
509 | 3526 | s->frame_pred_frame_dct = 1; | |
510 |
2/2✓ Branch 0 taken 3046 times.
✓ Branch 1 taken 480 times.
|
3526 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
511 | 3046 | put_header(s, EXT_START_CODE); | |
512 | 3046 | put_bits(&s->pb, 4, 8); /* pic ext */ | |
513 |
2/2✓ Branch 0 taken 919 times.
✓ Branch 1 taken 2127 times.
|
3046 | if (s->pict_type == AV_PICTURE_TYPE_P || |
514 |
2/2✓ Branch 0 taken 563 times.
✓ Branch 1 taken 356 times.
|
919 | s->pict_type == AV_PICTURE_TYPE_B) { |
515 | 2690 | put_bits(&s->pb, 4, s->f_code); | |
516 | 2690 | put_bits(&s->pb, 4, s->f_code); | |
517 | } else { | ||
518 | 356 | put_bits(&s->pb, 8, 255); | |
519 | } | ||
520 |
2/2✓ Branch 0 taken 563 times.
✓ Branch 1 taken 2483 times.
|
3046 | if (s->pict_type == AV_PICTURE_TYPE_B) { |
521 | 563 | put_bits(&s->pb, 4, s->b_code); | |
522 | 563 | put_bits(&s->pb, 4, s->b_code); | |
523 | } else { | ||
524 | 2483 | put_bits(&s->pb, 8, 255); | |
525 | } | ||
526 | 3046 | put_bits(&s->pb, 2, s->intra_dc_precision); | |
527 | |||
528 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3046 times.
|
3046 | av_assert0(s->picture_structure == PICT_FRAME); |
529 | 3046 | put_bits(&s->pb, 2, s->picture_structure); | |
530 |
2/2✓ Branch 0 taken 2096 times.
✓ Branch 1 taken 950 times.
|
3046 | if (s->progressive_sequence) |
531 | 2096 | put_bits(&s->pb, 1, 0); /* no repeat */ | |
532 | else | ||
533 | 950 | put_bits(&s->pb, 1, !!(s->current_picture_ptr->f->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)); | |
534 | /* XXX: optimize the generation of this flag with entropy measures */ | ||
535 | 3046 | s->frame_pred_frame_dct = s->progressive_sequence; | |
536 | |||
537 | 3046 | put_bits(&s->pb, 1, s->frame_pred_frame_dct); | |
538 | 3046 | put_bits(&s->pb, 1, s->concealment_motion_vectors); | |
539 | 3046 | put_bits(&s->pb, 1, s->q_scale_type); | |
540 | 3046 | put_bits(&s->pb, 1, s->intra_vlc_format); | |
541 | 3046 | put_bits(&s->pb, 1, s->alternate_scan); | |
542 | 3046 | put_bits(&s->pb, 1, s->repeat_first_field); | |
543 | 3046 | s->progressive_frame = s->progressive_sequence; | |
544 | /* chroma_420_type */ | ||
545 |
2/2✓ Branch 0 taken 2696 times.
✓ Branch 1 taken 350 times.
|
5742 | put_bits(&s->pb, 1, s->chroma_format == |
546 | 2696 | CHROMA_420 ? s->progressive_frame : 0); | |
547 | 3046 | put_bits(&s->pb, 1, s->progressive_frame); | |
548 | 3046 | put_bits(&s->pb, 1, 0); /* composite_display_flag */ | |
549 | } | ||
550 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3526 times.
|
3526 | if (mpeg12->scan_offset) { |
551 | int i; | ||
552 | |||
553 | ✗ | put_header(s, USER_START_CODE); | |
554 | ✗ | for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++) | |
555 | ✗ | put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]); | |
556 | } | ||
557 | 3526 | side_data = av_frame_get_side_data(s->current_picture_ptr->f, | |
558 | AV_FRAME_DATA_STEREO3D); | ||
559 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3526 times.
|
3526 | if (side_data) { |
560 | ✗ | AVStereo3D *stereo = (AVStereo3D *)side_data->data; | |
561 | uint8_t fpa_type; | ||
562 | |||
563 | ✗ | switch (stereo->type) { | |
564 | ✗ | case AV_STEREO3D_SIDEBYSIDE: | |
565 | ✗ | fpa_type = 0x03; | |
566 | ✗ | break; | |
567 | ✗ | case AV_STEREO3D_TOPBOTTOM: | |
568 | ✗ | fpa_type = 0x04; | |
569 | ✗ | break; | |
570 | ✗ | case AV_STEREO3D_2D: | |
571 | ✗ | fpa_type = 0x08; | |
572 | ✗ | break; | |
573 | ✗ | case AV_STEREO3D_SIDEBYSIDE_QUINCUNX: | |
574 | ✗ | fpa_type = 0x23; | |
575 | ✗ | break; | |
576 | ✗ | default: | |
577 | ✗ | fpa_type = 0; | |
578 | ✗ | break; | |
579 | } | ||
580 | |||
581 | ✗ | if (fpa_type != 0) { | |
582 | ✗ | put_header(s, USER_START_CODE); | |
583 | ✗ | put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier | |
584 | ✗ | put_bits(&s->pb, 8, 'P'); | |
585 | ✗ | put_bits(&s->pb, 8, '3'); | |
586 | ✗ | put_bits(&s->pb, 8, 'D'); | |
587 | ✗ | put_bits(&s->pb, 8, 0x03); // S3D_video_format_length | |
588 | |||
589 | ✗ | put_bits(&s->pb, 1, 1); // reserved_bit | |
590 | ✗ | put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type | |
591 | ✗ | put_bits(&s->pb, 8, 0x04); // reserved_data[0] | |
592 | ✗ | put_bits(&s->pb, 8, 0xFF); // reserved_data[1] | |
593 | } | ||
594 | } | ||
595 | |||
596 |
2/2✓ Branch 0 taken 3046 times.
✓ Branch 1 taken 480 times.
|
3526 | if (CONFIG_MPEG2VIDEO_ENCODER && mpeg12->a53_cc) { |
597 | 3046 | side_data = av_frame_get_side_data(s->current_picture_ptr->f, | |
598 | AV_FRAME_DATA_A53_CC); | ||
599 |
2/2✓ Branch 0 taken 135 times.
✓ Branch 1 taken 2911 times.
|
3046 | if (side_data) { |
600 |
3/4✓ Branch 0 taken 134 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
|
269 | if (side_data->size <= A53_MAX_CC_COUNT * 3 && side_data->size % 3 == 0) { |
601 | 134 | int i = 0; | |
602 | |||
603 | 134 | put_header (s, USER_START_CODE); | |
604 | |||
605 | 134 | put_bits(&s->pb, 8, 'G'); // user_identifier | |
606 | 134 | put_bits(&s->pb, 8, 'A'); | |
607 | 134 | put_bits(&s->pb, 8, '9'); | |
608 | 134 | put_bits(&s->pb, 8, '4'); | |
609 | 134 | put_bits(&s->pb, 8, 3); // user_data_type_code | |
610 | 134 | put_bits(&s->pb, 8, | |
611 | 134 | (side_data->size / 3 & A53_MAX_CC_COUNT) | 0x40); // flags, cc_count | |
612 | 134 | put_bits(&s->pb, 8, 0xff); // em_data | |
613 | |||
614 |
2/2✓ Branch 0 taken 8040 times.
✓ Branch 1 taken 134 times.
|
8174 | for (i = 0; i < side_data->size; i++) |
615 | 8040 | put_bits(&s->pb, 8, side_data->data[i]); | |
616 | |||
617 | 134 | put_bits(&s->pb, 8, 0xff); // marker_bits | |
618 | } else { | ||
619 | 1 | av_log(s->avctx, AV_LOG_WARNING, | |
620 | "Closed Caption size (%"SIZE_SPECIFIER") can not exceed " | ||
621 | "93 bytes and must be a multiple of 3\n", side_data->size); | ||
622 | } | ||
623 | } | ||
624 | } | ||
625 | |||
626 | 3526 | s->mb_y = 0; | |
627 | 3526 | ff_mpeg1_encode_slice_header(s); | |
628 | 3526 | } | |
629 | |||
630 | 1440443 | static inline void put_mb_modes(MpegEncContext *s, int n, int bits, | |
631 | int has_mv, int field_motion) | ||
632 | { | ||
633 | 1440443 | put_bits(&s->pb, n, bits); | |
634 |
2/2✓ Branch 0 taken 698812 times.
✓ Branch 1 taken 741631 times.
|
1440443 | if (!s->frame_pred_frame_dct) { |
635 |
2/2✓ Branch 0 taken 364218 times.
✓ Branch 1 taken 334594 times.
|
698812 | if (has_mv) |
636 | /* motion_type: frame/field */ | ||
637 | 364218 | put_bits(&s->pb, 2, 2 - field_motion); | |
638 | 698812 | put_bits(&s->pb, 1, s->interlaced_dct); | |
639 | } | ||
640 | 1440443 | } | |
641 | |||
642 | // RAL: Parameter added: f_or_b_code | ||
643 | 3252446 | static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code) | |
644 | { | ||
645 |
2/2✓ Branch 0 taken 1296693 times.
✓ Branch 1 taken 1955753 times.
|
3252446 | if (val == 0) { |
646 | /* zero vector, corresponds to ff_mpeg12_mbMotionVectorTable[0] */ | ||
647 | 1296693 | put_bits(&s->pb, 1, 0x01); | |
648 | } else { | ||
649 | int code, sign, bits; | ||
650 | 1955753 | int bit_size = f_or_b_code - 1; | |
651 | 1955753 | int range = 1 << bit_size; | |
652 | /* modulo encoding */ | ||
653 | 1955753 | val = sign_extend(val, 5 + bit_size); | |
654 | |||
655 |
2/2✓ Branch 0 taken 943197 times.
✓ Branch 1 taken 1012556 times.
|
1955753 | if (val >= 0) { |
656 | 943197 | val--; | |
657 | 943197 | code = (val >> bit_size) + 1; | |
658 | 943197 | bits = val & (range - 1); | |
659 | 943197 | sign = 0; | |
660 | } else { | ||
661 | 1012556 | val = -val; | |
662 | 1012556 | val--; | |
663 | 1012556 | code = (val >> bit_size) + 1; | |
664 | 1012556 | bits = val & (range - 1); | |
665 | 1012556 | sign = 1; | |
666 | } | ||
667 | |||
668 | av_assert2(code > 0 && code <= 16); | ||
669 | |||
670 | 1955753 | put_bits(&s->pb, | |
671 | 1955753 | ff_mpeg12_mbMotionVectorTable[code][1], | |
672 | 1955753 | ff_mpeg12_mbMotionVectorTable[code][0]); | |
673 | |||
674 | 1955753 | put_bits(&s->pb, 1, sign); | |
675 |
2/2✓ Branch 0 taken 1405934 times.
✓ Branch 1 taken 549819 times.
|
1955753 | if (bit_size > 0) |
676 | 1405934 | put_bits(&s->pb, bit_size, bits); | |
677 | } | ||
678 | 3252446 | } | |
679 | |||
680 | 3420302 | static inline void encode_dc(MpegEncContext *s, int diff, int component) | |
681 | { | ||
682 | 3420302 | unsigned int diff_u = diff + 255; | |
683 |
2/2✓ Branch 0 taken 162210 times.
✓ Branch 1 taken 3258092 times.
|
3420302 | if (diff_u >= 511) { |
684 | int index; | ||
685 | |||
686 |
2/2✓ Branch 0 taken 113286 times.
✓ Branch 1 taken 48924 times.
|
162210 | if (diff < 0) { |
687 | 113286 | index = av_log2_16bit(-2 * diff); | |
688 | 113286 | diff--; | |
689 | } else { | ||
690 | 48924 | index = av_log2_16bit(2 * diff); | |
691 | } | ||
692 |
2/2✓ Branch 0 taken 91380 times.
✓ Branch 1 taken 70830 times.
|
162210 | if (component == 0) |
693 | 91380 | put_bits(&s->pb, | |
694 | 91380 | ff_mpeg12_vlc_dc_lum_bits[index] + index, | |
695 | 91380 | (ff_mpeg12_vlc_dc_lum_code[index] << index) + | |
696 | 91380 | av_mod_uintp2(diff, index)); | |
697 | else | ||
698 | 70830 | put_bits(&s->pb, | |
699 | 70830 | ff_mpeg12_vlc_dc_chroma_bits[index] + index, | |
700 | 70830 | (ff_mpeg12_vlc_dc_chroma_code[index] << index) + | |
701 | 70830 | av_mod_uintp2(diff, index)); | |
702 | } else { | ||
703 |
2/2✓ Branch 0 taken 1795140 times.
✓ Branch 1 taken 1462952 times.
|
3258092 | if (component == 0) |
704 | 1795140 | put_bits(&s->pb, | |
705 | 1795140 | mpeg1_lum_dc_uni[diff + 255] & 0xFF, | |
706 | 1795140 | mpeg1_lum_dc_uni[diff + 255] >> 8); | |
707 | else | ||
708 | 1462952 | put_bits(&s->pb, | |
709 | 1462952 | mpeg1_chr_dc_uni[diff + 255] & 0xFF, | |
710 | 1462952 | mpeg1_chr_dc_uni[diff + 255] >> 8); | |
711 | } | ||
712 | 3420302 | } | |
713 | |||
714 | 6954396 | static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n) | |
715 | { | ||
716 | int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; | ||
717 | int code, component; | ||
718 | 6954396 | const uint16_t (*table_vlc)[2] = ff_mpeg1_vlc_table; | |
719 | |||
720 | 6954396 | last_index = s->block_last_index[n]; | |
721 | |||
722 | /* DC coef */ | ||
723 |
2/2✓ Branch 0 taken 3420302 times.
✓ Branch 1 taken 3534094 times.
|
6954396 | if (s->mb_intra) { |
724 |
2/2✓ Branch 0 taken 1533782 times.
✓ Branch 1 taken 1886520 times.
|
3420302 | component = (n <= 3 ? 0 : (n & 1) + 1); |
725 | 3420302 | dc = block[0]; /* overflow is impossible */ | |
726 | 3420302 | diff = dc - s->last_dc[component]; | |
727 | 3420302 | encode_dc(s, diff, component); | |
728 | 3420302 | s->last_dc[component] = dc; | |
729 | 3420302 | i = 1; | |
730 |
2/2✓ Branch 0 taken 2641700 times.
✓ Branch 1 taken 778602 times.
|
3420302 | if (s->intra_vlc_format) |
731 | 2641700 | table_vlc = ff_mpeg2_vlc_table; | |
732 | } else { | ||
733 | /* encode the first coefficient: needs to be done here because | ||
734 | * it is handled slightly differently */ | ||
735 | 3534094 | level = block[0]; | |
736 |
2/2✓ Branch 0 taken 1056601 times.
✓ Branch 1 taken 2477493 times.
|
3534094 | if (abs(level) == 1) { |
737 | 1056601 | code = ((uint32_t)level >> 31); /* the sign bit */ | |
738 | 1056601 | put_bits(&s->pb, 2, code | 0x02); | |
739 | 1056601 | i = 1; | |
740 | } else { | ||
741 | 2477493 | i = 0; | |
742 | 2477493 | last_non_zero = -1; | |
743 | 2477493 | goto next_coef; | |
744 | } | ||
745 | } | ||
746 | |||
747 | /* now quantify & encode AC coefs */ | ||
748 | 4476903 | last_non_zero = i - 1; | |
749 | |||
750 |
2/2✓ Branch 0 taken 162069163 times.
✓ Branch 1 taken 6954396 times.
|
169023559 | for (; i <= last_index; i++) { |
751 | 162069163 | j = s->intra_scantable.permutated[i]; | |
752 | 162069163 | level = block[j]; | |
753 | |||
754 | 164546656 | next_coef: | |
755 | /* encode using VLC */ | ||
756 |
2/2✓ Branch 0 taken 68417734 times.
✓ Branch 1 taken 96128922 times.
|
164546656 | if (level != 0) { |
757 | 68417734 | run = i - last_non_zero - 1; | |
758 | |||
759 | 68417734 | alevel = level; | |
760 | 68417734 | MASK_ABS(sign, alevel); | |
761 | 68417734 | sign &= 1; | |
762 | |||
763 |
2/2✓ Branch 0 taken 66258771 times.
✓ Branch 1 taken 2158963 times.
|
68417734 | if (alevel <= mpeg12_max_level[run]) { |
764 | 66258771 | code = mpeg12_index_run[run] + alevel - 1; | |
765 | /* store the VLC & sign at once */ | ||
766 | 66258771 | put_bits(&s->pb, table_vlc[code][1] + 1, | |
767 | 66258771 | (table_vlc[code][0] << 1) + sign); | |
768 | } else { | ||
769 | /* Escape seems to be pretty rare <5% so I do not optimize it; | ||
770 | * the following value is the common escape value for both | ||
771 | * possible tables (i.e. table_vlc[111]). */ | ||
772 | 2158963 | put_bits(&s->pb, 6, 0x01); | |
773 | /* escape: only clip in this case */ | ||
774 | 2158963 | put_bits(&s->pb, 6, run); | |
775 |
2/2✓ Branch 0 taken 82680 times.
✓ Branch 1 taken 2076283 times.
|
2158963 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) { |
776 |
1/2✓ Branch 0 taken 82680 times.
✗ Branch 1 not taken.
|
82680 | if (alevel < 128) { |
777 | 82680 | put_sbits(&s->pb, 8, level); | |
778 | } else { | ||
779 | ✗ | if (level < 0) | |
780 | ✗ | put_bits(&s->pb, 16, 0x8001 + level + 255); | |
781 | else | ||
782 | ✗ | put_sbits(&s->pb, 16, level); | |
783 | } | ||
784 | } else { | ||
785 | 2076283 | put_sbits(&s->pb, 12, level); | |
786 | } | ||
787 | } | ||
788 | 68417734 | last_non_zero = i; | |
789 | } | ||
790 | } | ||
791 | /* end of block */ | ||
792 | 6954396 | put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]); | |
793 | 6954396 | } | |
794 | |||
795 | 1890528 | static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s, | |
796 | int16_t block[8][64], | ||
797 | int motion_x, int motion_y, | ||
798 | int mb_block_count, | ||
799 | int chroma_y_shift) | ||
800 | { | ||
801 | /* MPEG-1 is always 420. */ | ||
802 | #define IS_MPEG1(s) (chroma_y_shift == 1 && (s)->codec_id == AV_CODEC_ID_MPEG1VIDEO) | ||
803 | int i, cbp; | ||
804 | 1890528 | const int mb_x = s->mb_x; | |
805 | 1890528 | const int mb_y = s->mb_y; | |
806 |
4/4✓ Branch 0 taken 330149 times.
✓ Branch 1 taken 1560379 times.
✓ Branch 2 taken 325492 times.
✓ Branch 3 taken 4657 times.
|
1890528 | const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y; |
807 | |||
808 | /* compute cbp */ | ||
809 | 1890528 | cbp = 0; | |
810 |
2/2✓ Branch 0 taken 12629078 times.
✓ Branch 1 taken 1890528 times.
|
14519606 | for (i = 0; i < mb_block_count; i++) |
811 |
2/2✓ Branch 0 taken 6954396 times.
✓ Branch 1 taken 5674682 times.
|
12629078 | if (s->block_last_index[i] >= 0) |
812 | 6954396 | cbp |= 1 << (mb_block_count - 1 - i); | |
813 | |||
814 |
6/6✓ Branch 0 taken 450085 times.
✓ Branch 1 taken 1440443 times.
✓ Branch 2 taken 438853 times.
✓ Branch 3 taken 11232 times.
✓ Branch 4 taken 418630 times.
✓ Branch 5 taken 20223 times.
|
1890528 | if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 && |
815 |
2/2✓ Branch 0 taken 12059 times.
✓ Branch 1 taken 406571 times.
|
418630 | (mb_x != s->mb_width - 1 || |
816 |
6/6✓ Branch 0 taken 11306 times.
✓ Branch 1 taken 753 times.
✓ Branch 2 taken 9346 times.
✓ Branch 3 taken 1960 times.
✓ Branch 4 taken 552 times.
✓ Branch 5 taken 8794 times.
|
12059 | (mb_y != s->end_mb_y - 1 && IS_MPEG1(s))) && |
817 |
4/4✓ Branch 0 taken 236887 times.
✓ Branch 1 taken 170236 times.
✓ Branch 2 taken 108550 times.
✓ Branch 3 taken 128337 times.
|
407123 | ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) || |
818 |
4/4✓ Branch 0 taken 170236 times.
✓ Branch 1 taken 108550 times.
✓ Branch 2 taken 22008 times.
✓ Branch 3 taken 148228 times.
|
278786 | (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir && |
819 | 22008 | (((s->mv_dir & MV_DIR_FORWARD) | |
820 | 20311 | ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) | | |
821 |
4/4✓ Branch 0 taken 20311 times.
✓ Branch 1 taken 1697 times.
✓ Branch 2 taken 4342 times.
✓ Branch 3 taken 17666 times.
|
44016 | (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | |
822 | 22008 | ((s->mv_dir & MV_DIR_BACKWARD) | |
823 | 21679 | ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) | | |
824 |
2/2✓ Branch 0 taken 21679 times.
✓ Branch 1 taken 329 times.
|
22008 | (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { |
825 | 132679 | s->mb_skip_run++; | |
826 | 132679 | s->qscale -= s->dquant; | |
827 | 132679 | s->skip_count++; | |
828 | 132679 | s->misc_bits++; | |
829 | 132679 | s->last_bits++; | |
830 |
2/2✓ Branch 0 taken 128337 times.
✓ Branch 1 taken 4342 times.
|
132679 | if (s->pict_type == AV_PICTURE_TYPE_P) { |
831 | 128337 | s->last_mv[0][0][0] = | |
832 | 128337 | s->last_mv[0][0][1] = | |
833 | 128337 | s->last_mv[0][1][0] = | |
834 | 128337 | s->last_mv[0][1][1] = 0; | |
835 | } | ||
836 | } else { | ||
837 |
2/2✓ Branch 0 taken 325492 times.
✓ Branch 1 taken 1432357 times.
|
1757849 | if (first_mb) { |
838 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 325492 times.
|
325492 | av_assert0(s->mb_skip_run == 0); |
839 | 325492 | encode_mb_skip_run(s, s->mb_x); | |
840 | } else { | ||
841 | 1432357 | encode_mb_skip_run(s, s->mb_skip_run); | |
842 | } | ||
843 | |||
844 |
2/2✓ Branch 0 taken 398956 times.
✓ Branch 1 taken 1358893 times.
|
1757849 | if (s->pict_type == AV_PICTURE_TYPE_I) { |
845 |
3/4✓ Branch 0 taken 43090 times.
✓ Branch 1 taken 355866 times.
✓ Branch 2 taken 43090 times.
✗ Branch 3 not taken.
|
398956 | if (s->dquant && cbp) { |
846 | /* macroblock_type: macroblock_quant = 1 */ | ||
847 | 43090 | put_mb_modes(s, 2, 1, 0, 0); | |
848 | 43090 | put_qscale(s); | |
849 | } else { | ||
850 | /* macroblock_type: macroblock_quant = 0 */ | ||
851 | 355866 | put_mb_modes(s, 1, 1, 0, 0); | |
852 | 355866 | s->qscale -= s->dquant; | |
853 | } | ||
854 | 398956 | s->misc_bits += get_bits_diff(s); | |
855 | 398956 | s->i_count++; | |
856 |
2/2✓ Branch 0 taken 72674 times.
✓ Branch 1 taken 1286219 times.
|
1358893 | } else if (s->mb_intra) { |
857 |
3/4✓ Branch 0 taken 11835 times.
✓ Branch 1 taken 60839 times.
✓ Branch 2 taken 11835 times.
✗ Branch 3 not taken.
|
72674 | if (s->dquant && cbp) { |
858 | 11835 | put_mb_modes(s, 6, 0x01, 0, 0); | |
859 | 11835 | put_qscale(s); | |
860 | } else { | ||
861 | 60839 | put_mb_modes(s, 5, 0x03, 0, 0); | |
862 | 60839 | s->qscale -= s->dquant; | |
863 | } | ||
864 | 72674 | s->misc_bits += get_bits_diff(s); | |
865 | 72674 | s->i_count++; | |
866 | 72674 | memset(s->last_mv, 0, sizeof(s->last_mv)); | |
867 |
2/2✓ Branch 0 taken 705276 times.
✓ Branch 1 taken 580943 times.
|
1286219 | } else if (s->pict_type == AV_PICTURE_TYPE_P) { |
868 |
2/2✓ Branch 0 taken 682427 times.
✓ Branch 1 taken 22849 times.
|
705276 | if (s->mv_type == MV_TYPE_16X16) { |
869 |
2/2✓ Branch 0 taken 561177 times.
✓ Branch 1 taken 121250 times.
|
682427 | if (cbp != 0) { |
870 |
2/2✓ Branch 0 taken 60866 times.
✓ Branch 1 taken 500311 times.
|
561177 | if ((motion_x | motion_y) == 0) { |
871 |
2/2✓ Branch 0 taken 1554 times.
✓ Branch 1 taken 59312 times.
|
60866 | if (s->dquant) { |
872 | /* macroblock_pattern & quant */ | ||
873 | 1554 | put_mb_modes(s, 5, 1, 0, 0); | |
874 | 1554 | put_qscale(s); | |
875 | } else { | ||
876 | /* macroblock_pattern only */ | ||
877 | 59312 | put_mb_modes(s, 2, 1, 0, 0); | |
878 | } | ||
879 | 60866 | s->misc_bits += get_bits_diff(s); | |
880 | } else { | ||
881 |
2/2✓ Branch 0 taken 72388 times.
✓ Branch 1 taken 427923 times.
|
500311 | if (s->dquant) { |
882 | 72388 | put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */ | |
883 | 72388 | put_qscale(s); | |
884 | } else { | ||
885 | 427923 | put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */ | |
886 | } | ||
887 | 500311 | s->misc_bits += get_bits_diff(s); | |
888 | // RAL: f_code parameter added | ||
889 | 500311 | mpeg1_encode_motion(s, | |
890 | 500311 | motion_x - s->last_mv[0][0][0], | |
891 | s->f_code); | ||
892 | // RAL: f_code parameter added | ||
893 | 500311 | mpeg1_encode_motion(s, | |
894 | 500311 | motion_y - s->last_mv[0][0][1], | |
895 | s->f_code); | ||
896 | 500311 | s->mv_bits += get_bits_diff(s); | |
897 | } | ||
898 | } else { | ||
899 | 121250 | put_bits(&s->pb, 3, 1); /* motion only */ | |
900 |
2/2✓ Branch 0 taken 17533 times.
✓ Branch 1 taken 103717 times.
|
121250 | if (!s->frame_pred_frame_dct) |
901 | 17533 | put_bits(&s->pb, 2, 2); /* motion_type: frame */ | |
902 | 121250 | s->misc_bits += get_bits_diff(s); | |
903 | // RAL: f_code parameter added | ||
904 | 121250 | mpeg1_encode_motion(s, | |
905 | 121250 | motion_x - s->last_mv[0][0][0], | |
906 | s->f_code); | ||
907 | // RAL: f_code parameter added | ||
908 | 121250 | mpeg1_encode_motion(s, | |
909 | 121250 | motion_y - s->last_mv[0][0][1], | |
910 | s->f_code); | ||
911 | 121250 | s->qscale -= s->dquant; | |
912 | 121250 | s->mv_bits += get_bits_diff(s); | |
913 | } | ||
914 | 682427 | s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x; | |
915 | 682427 | s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y; | |
916 | } else { | ||
917 | av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD); | ||
918 | |||
919 |
2/2✓ Branch 0 taken 21344 times.
✓ Branch 1 taken 1505 times.
|
22849 | if (cbp) { |
920 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21344 times.
|
21344 | if (s->dquant) { |
921 | ✗ | put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */ | |
922 | ✗ | put_qscale(s); | |
923 | } else { | ||
924 | 21344 | put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */ | |
925 | } | ||
926 | } else { | ||
927 | 1505 | put_bits(&s->pb, 3, 1); /* motion only */ | |
928 | 1505 | put_bits(&s->pb, 2, 1); /* motion_type: field */ | |
929 | 1505 | s->qscale -= s->dquant; | |
930 | } | ||
931 | 22849 | s->misc_bits += get_bits_diff(s); | |
932 |
2/2✓ Branch 0 taken 45698 times.
✓ Branch 1 taken 22849 times.
|
68547 | for (i = 0; i < 2; i++) { |
933 | 45698 | put_bits(&s->pb, 1, s->field_select[0][i]); | |
934 | 45698 | mpeg1_encode_motion(s, | |
935 | 45698 | s->mv[0][i][0] - s->last_mv[0][i][0], | |
936 | s->f_code); | ||
937 | 45698 | mpeg1_encode_motion(s, | |
938 | 45698 | s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1), | |
939 | s->f_code); | ||
940 | 45698 | s->last_mv[0][i][0] = s->mv[0][i][0]; | |
941 | 45698 | s->last_mv[0][i][1] = 2 * s->mv[0][i][1]; | |
942 | } | ||
943 | 22849 | s->mv_bits += get_bits_diff(s); | |
944 | } | ||
945 |
2/2✓ Branch 0 taken 582521 times.
✓ Branch 1 taken 122755 times.
|
705276 | if (cbp) { |
946 |
2/2✓ Branch 0 taken 507709 times.
✓ Branch 1 taken 74812 times.
|
582521 | if (chroma_y_shift) { |
947 | 507709 | put_bits(&s->pb, | |
948 | 507709 | ff_mpeg12_mbPatTable[cbp][1], | |
949 | 507709 | ff_mpeg12_mbPatTable[cbp][0]); | |
950 | } else { | ||
951 | 74812 | put_bits(&s->pb, | |
952 | 74812 | ff_mpeg12_mbPatTable[cbp >> 2][1], | |
953 | 74812 | ff_mpeg12_mbPatTable[cbp >> 2][0]); | |
954 | 74812 | put_sbits(&s->pb, 2, cbp); | |
955 | } | ||
956 | } | ||
957 | } else { | ||
958 |
2/2✓ Branch 0 taken 488110 times.
✓ Branch 1 taken 92833 times.
|
580943 | if (s->mv_type == MV_TYPE_16X16) { |
959 |
2/2✓ Branch 0 taken 312597 times.
✓ Branch 1 taken 175513 times.
|
488110 | if (cbp) { // With coded bloc pattern |
960 |
2/2✓ Branch 0 taken 53290 times.
✓ Branch 1 taken 259307 times.
|
312597 | if (s->dquant) { |
961 |
2/2✓ Branch 0 taken 11218 times.
✓ Branch 1 taken 42072 times.
|
53290 | if (s->mv_dir == MV_DIR_FORWARD) |
962 | 11218 | put_mb_modes(s, 6, 3, 1, 0); | |
963 | else | ||
964 | 42072 | put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0); | |
965 | 53290 | put_qscale(s); | |
966 | } else { | ||
967 | 259307 | put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0); | |
968 | } | ||
969 | } else { // No coded bloc pattern | ||
970 | 175513 | put_bits(&s->pb, 5 - s->mv_dir, 2); | |
971 |
2/2✓ Branch 0 taken 82021 times.
✓ Branch 1 taken 93492 times.
|
175513 | if (!s->frame_pred_frame_dct) |
972 | 82021 | put_bits(&s->pb, 2, 2); /* motion_type: frame */ | |
973 | 175513 | s->qscale -= s->dquant; | |
974 | } | ||
975 | 488110 | s->misc_bits += get_bits_diff(s); | |
976 |
2/2✓ Branch 0 taken 351307 times.
✓ Branch 1 taken 136803 times.
|
488110 | if (s->mv_dir & MV_DIR_FORWARD) { |
977 | 351307 | mpeg1_encode_motion(s, | |
978 | 351307 | s->mv[0][0][0] - s->last_mv[0][0][0], | |
979 | s->f_code); | ||
980 | 351307 | mpeg1_encode_motion(s, | |
981 | 351307 | s->mv[0][0][1] - s->last_mv[0][0][1], | |
982 | s->f_code); | ||
983 | 351307 | s->last_mv[0][0][0] = | |
984 | 351307 | s->last_mv[0][1][0] = s->mv[0][0][0]; | |
985 | 351307 | s->last_mv[0][0][1] = | |
986 | 351307 | s->last_mv[0][1][1] = s->mv[0][0][1]; | |
987 | } | ||
988 |
2/2✓ Branch 0 taken 369937 times.
✓ Branch 1 taken 118173 times.
|
488110 | if (s->mv_dir & MV_DIR_BACKWARD) { |
989 | 369937 | mpeg1_encode_motion(s, | |
990 | 369937 | s->mv[1][0][0] - s->last_mv[1][0][0], | |
991 | s->b_code); | ||
992 | 369937 | mpeg1_encode_motion(s, | |
993 | 369937 | s->mv[1][0][1] - s->last_mv[1][0][1], | |
994 | s->b_code); | ||
995 | 369937 | s->last_mv[1][0][0] = | |
996 | 369937 | s->last_mv[1][1][0] = s->mv[1][0][0]; | |
997 | 369937 | s->last_mv[1][0][1] = | |
998 | 369937 | s->last_mv[1][1][1] = s->mv[1][0][1]; | |
999 | } | ||
1000 | } else { | ||
1001 | av_assert2(s->mv_type == MV_TYPE_FIELD); | ||
1002 | av_assert2(!s->frame_pred_frame_dct); | ||
1003 |
2/2✓ Branch 0 taken 73695 times.
✓ Branch 1 taken 19138 times.
|
92833 | if (cbp) { // With coded bloc pattern |
1004 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 73695 times.
|
73695 | if (s->dquant) { |
1005 | ✗ | if (s->mv_dir == MV_DIR_FORWARD) | |
1006 | ✗ | put_mb_modes(s, 6, 3, 1, 1); | |
1007 | else | ||
1008 | ✗ | put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1); | |
1009 | ✗ | put_qscale(s); | |
1010 | } else { | ||
1011 | 73695 | put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1); | |
1012 | } | ||
1013 | } else { // No coded bloc pattern | ||
1014 | 19138 | put_bits(&s->pb, 5 - s->mv_dir, 2); | |
1015 | 19138 | put_bits(&s->pb, 2, 1); /* motion_type: field */ | |
1016 | 19138 | s->qscale -= s->dquant; | |
1017 | } | ||
1018 | 92833 | s->misc_bits += get_bits_diff(s); | |
1019 |
2/2✓ Branch 0 taken 59191 times.
✓ Branch 1 taken 33642 times.
|
92833 | if (s->mv_dir & MV_DIR_FORWARD) { |
1020 |
2/2✓ Branch 0 taken 118382 times.
✓ Branch 1 taken 59191 times.
|
177573 | for (i = 0; i < 2; i++) { |
1021 | 118382 | put_bits(&s->pb, 1, s->field_select[0][i]); | |
1022 | 118382 | mpeg1_encode_motion(s, | |
1023 | 118382 | s->mv[0][i][0] - s->last_mv[0][i][0], | |
1024 | s->f_code); | ||
1025 | 118382 | mpeg1_encode_motion(s, | |
1026 | 118382 | s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1), | |
1027 | s->f_code); | ||
1028 | 118382 | s->last_mv[0][i][0] = s->mv[0][i][0]; | |
1029 | 118382 | s->last_mv[0][i][1] = s->mv[0][i][1] * 2; | |
1030 | } | ||
1031 | } | ||
1032 |
2/2✓ Branch 0 taken 59669 times.
✓ Branch 1 taken 33164 times.
|
92833 | if (s->mv_dir & MV_DIR_BACKWARD) { |
1033 |
2/2✓ Branch 0 taken 119338 times.
✓ Branch 1 taken 59669 times.
|
179007 | for (i = 0; i < 2; i++) { |
1034 | 119338 | put_bits(&s->pb, 1, s->field_select[1][i]); | |
1035 | 119338 | mpeg1_encode_motion(s, | |
1036 | 119338 | s->mv[1][i][0] - s->last_mv[1][i][0], | |
1037 | s->b_code); | ||
1038 | 119338 | mpeg1_encode_motion(s, | |
1039 | 119338 | s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1), | |
1040 | s->b_code); | ||
1041 | 119338 | s->last_mv[1][i][0] = s->mv[1][i][0]; | |
1042 | 119338 | s->last_mv[1][i][1] = s->mv[1][i][1] * 2; | |
1043 | } | ||
1044 | } | ||
1045 | } | ||
1046 | 580943 | s->mv_bits += get_bits_diff(s); | |
1047 |
2/2✓ Branch 0 taken 386292 times.
✓ Branch 1 taken 194651 times.
|
580943 | if (cbp) { |
1048 |
2/2✓ Branch 0 taken 206130 times.
✓ Branch 1 taken 180162 times.
|
386292 | if (chroma_y_shift) { |
1049 | 206130 | put_bits(&s->pb, | |
1050 | 206130 | ff_mpeg12_mbPatTable[cbp][1], | |
1051 | 206130 | ff_mpeg12_mbPatTable[cbp][0]); | |
1052 | } else { | ||
1053 | 180162 | put_bits(&s->pb, | |
1054 | 180162 | ff_mpeg12_mbPatTable[cbp >> 2][1], | |
1055 | 180162 | ff_mpeg12_mbPatTable[cbp >> 2][0]); | |
1056 | 180162 | put_sbits(&s->pb, 2, cbp); | |
1057 | } | ||
1058 | } | ||
1059 | } | ||
1060 |
2/2✓ Branch 0 taken 11831708 times.
✓ Branch 1 taken 1757849 times.
|
13589557 | for (i = 0; i < mb_block_count; i++) |
1061 |
2/2✓ Branch 0 taken 6954396 times.
✓ Branch 1 taken 4877312 times.
|
11831708 | if (cbp & (1 << (mb_block_count - 1 - i))) |
1062 | 6954396 | mpeg1_encode_block(s, block[i], i); | |
1063 | 1757849 | s->mb_skip_run = 0; | |
1064 |
2/2✓ Branch 0 taken 471630 times.
✓ Branch 1 taken 1286219 times.
|
1757849 | if (s->mb_intra) |
1065 | 471630 | s->i_tex_bits += get_bits_diff(s); | |
1066 | else | ||
1067 | 1286219 | s->p_tex_bits += get_bits_diff(s); | |
1068 | } | ||
1069 | 1890528 | } | |
1070 | |||
1071 | 1890528 | void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64], | |
1072 | int motion_x, int motion_y) | ||
1073 | { | ||
1074 |
2/2✓ Branch 0 taken 1247573 times.
✓ Branch 1 taken 642955 times.
|
1890528 | if (s->chroma_format == CHROMA_420) |
1075 | 1247573 | mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6, 1); | |
1076 | else | ||
1077 | 642955 | mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8, 0); | |
1078 | 1890528 | } | |
1079 | |||
1080 | 54 | static av_cold void mpeg12_encode_init_static(void) | |
1081 | { | ||
1082 | 54 | ff_rl_init_level_run(mpeg12_max_level, mpeg12_index_run, | |
1083 | ff_mpeg12_run, ff_mpeg12_level, MPEG12_RL_NB_ELEMS); | ||
1084 | |||
1085 | 54 | ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run, | |
1086 | ff_mpeg1_vlc_table, uni_mpeg1_ac_vlc_len); | ||
1087 | 54 | ff_mpeg1_init_uni_ac_vlc(mpeg12_max_level, mpeg12_index_run, | |
1088 | ff_mpeg2_vlc_table, uni_mpeg2_ac_vlc_len); | ||
1089 | |||
1090 | /* build unified dc encoding tables */ | ||
1091 |
2/2✓ Branch 0 taken 27594 times.
✓ Branch 1 taken 54 times.
|
27648 | for (int i = -255; i < 256; i++) { |
1092 | int adiff, index; | ||
1093 | int bits, code; | ||
1094 | 27594 | int diff = i; | |
1095 | |||
1096 | 27594 | adiff = FFABS(diff); | |
1097 |
2/2✓ Branch 0 taken 13770 times.
✓ Branch 1 taken 13824 times.
|
27594 | if (diff < 0) |
1098 | 13770 | diff--; | |
1099 | 27594 | index = av_log2(2 * adiff); | |
1100 | |||
1101 | 27594 | bits = ff_mpeg12_vlc_dc_lum_bits[index] + index; | |
1102 | 27594 | code = (ff_mpeg12_vlc_dc_lum_code[index] << index) + | |
1103 | 27594 | av_mod_uintp2(diff, index); | |
1104 | 27594 | mpeg1_lum_dc_uni[i + 255] = bits + (code << 8); | |
1105 | |||
1106 | 27594 | bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index; | |
1107 | 27594 | code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) + | |
1108 | 27594 | av_mod_uintp2(diff, index); | |
1109 | 27594 | mpeg1_chr_dc_uni[i + 255] = bits + (code << 8); | |
1110 | } | ||
1111 | |||
1112 |
2/2✓ Branch 0 taken 378 times.
✓ Branch 1 taken 54 times.
|
432 | for (int f_code = 1; f_code <= MAX_FCODE; f_code++) |
1113 |
2/2✓ Branch 0 taken 6193530 times.
✓ Branch 1 taken 378 times.
|
6193908 | for (int mv = -MAX_DMV; mv <= MAX_DMV; mv++) { |
1114 | int len; | ||
1115 | |||
1116 |
2/2✓ Branch 0 taken 378 times.
✓ Branch 1 taken 6193152 times.
|
6193530 | if (mv == 0) { |
1117 | 378 | len = 1; /* ff_mpeg12_mbMotionVectorTable[0][1] */ | |
1118 | } else { | ||
1119 | int val, bit_size, code; | ||
1120 | |||
1121 | 6193152 | bit_size = f_code - 1; | |
1122 | |||
1123 | 6193152 | val = mv; | |
1124 |
2/2✓ Branch 0 taken 3096576 times.
✓ Branch 1 taken 3096576 times.
|
6193152 | if (val < 0) |
1125 | 3096576 | val = -val; | |
1126 | 6193152 | val--; | |
1127 | 6193152 | code = (val >> bit_size) + 1; | |
1128 |
2/2✓ Branch 0 taken 219456 times.
✓ Branch 1 taken 5973696 times.
|
6193152 | if (code < 17) |
1129 | 219456 | len = ff_mpeg12_mbMotionVectorTable[code][1] + | |
1130 | 1 + bit_size; | ||
1131 | else | ||
1132 | 5973696 | len = 10 /* ff_mpeg12_mbMotionVectorTable[16][1] */ + | |
1133 | 2 + bit_size; | ||
1134 | } | ||
1135 | |||
1136 | 6193530 | mv_penalty[f_code][mv + MAX_DMV] = len; | |
1137 | } | ||
1138 | |||
1139 | |||
1140 |
2/2✓ Branch 0 taken 378 times.
✓ Branch 1 taken 54 times.
|
432 | for (int f_code = MAX_FCODE; f_code > 0; f_code--) |
1141 |
2/2✓ Branch 0 taken 219456 times.
✓ Branch 1 taken 378 times.
|
219834 | for (int mv = -(8 << f_code); mv < (8 << f_code); mv++) |
1142 | 219456 | fcode_tab[mv + MAX_MV] = f_code; | |
1143 | 54 | } | |
1144 | |||
1145 | 54 | av_cold void ff_mpeg1_encode_init(MpegEncContext *s) | |
1146 | { | ||
1147 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
1148 | |||
1149 | 54 | s->y_dc_scale_table = | |
1150 | 54 | s->c_dc_scale_table = ff_mpeg12_dc_scale_table[s->intra_dc_precision]; | |
1151 | |||
1152 | 54 | s->me.mv_penalty = mv_penalty; | |
1153 | 54 | s->fcode_tab = fcode_tab; | |
1154 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 43 times.
|
54 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) { |
1155 | 11 | s->min_qcoeff = -255; | |
1156 | 11 | s->max_qcoeff = 255; | |
1157 | } else { | ||
1158 | 43 | s->min_qcoeff = -2047; | |
1159 | 43 | s->max_qcoeff = 2047; | |
1160 | 43 | s->mpeg_quant = 1; | |
1161 | } | ||
1162 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 41 times.
|
54 | if (s->intra_vlc_format) { |
1163 | 13 | s->intra_ac_vlc_length = | |
1164 | 13 | s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len; | |
1165 | } else { | ||
1166 | 41 | s->intra_ac_vlc_length = | |
1167 | 41 | s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len; | |
1168 | } | ||
1169 | 54 | s->inter_ac_vlc_length = | |
1170 | 54 | s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len; | |
1171 | |||
1172 | 54 | ff_thread_once(&init_static_once, mpeg12_encode_init_static); | |
1173 | 54 | } | |
1174 | |||
1175 | #define OFFSET(x) offsetof(MPEG12EncContext, x) | ||
1176 | #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM | ||
1177 | #define COMMON_OPTS \ | ||
1178 | { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format. Overrides timecode_frame_start.", \ | ||
1179 | OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE },\ | ||
1180 | { "drop_frame_timecode", "Timecode is in drop frame format.", \ | ||
1181 | OFFSET(drop_frame_timecode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \ | ||
1182 | { "scan_offset", "Reserve space for SVCD scan offset user data.", \ | ||
1183 | OFFSET(scan_offset), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \ | ||
1184 | { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \ | ||
1185 | OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = -1 }, -1, INT64_MAX, VE}, \ | ||
1186 | FF_MPV_COMMON_BFRAME_OPTS | ||
1187 | |||
1188 | static const AVOption mpeg1_options[] = { | ||
1189 | COMMON_OPTS | ||
1190 | FF_MPV_COMMON_OPTS | ||
1191 | FF_MPV_COMMON_MOTION_EST_OPTS | ||
1192 | { NULL }, | ||
1193 | }; | ||
1194 | |||
1195 | static const AVOption mpeg2_options[] = { | ||
1196 | COMMON_OPTS | ||
1197 | { "intra_vlc", "Use MPEG-2 intra VLC table.", | ||
1198 | FF_MPV_OFFSET(intra_vlc_format), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1199 | { "non_linear_quant", "Use nonlinear quantizer.", FF_MPV_OFFSET(q_scale_type), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1200 | { "alternate_scan", "Enable alternate scantable.", FF_MPV_OFFSET(alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | ||
1201 | { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE }, | ||
1202 | { "seq_disp_ext", "Write sequence_display_extension blocks.", OFFSET(seq_disp_ext), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, "seq_disp_ext" }, | ||
1203 | { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, "seq_disp_ext" }, | ||
1204 | { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, "seq_disp_ext" }, | ||
1205 | { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, "seq_disp_ext" }, | ||
1206 | { "video_format", "Video_format in the sequence_display_extension indicating the source of the video.", OFFSET(video_format), AV_OPT_TYPE_INT, { .i64 = VIDEO_FORMAT_UNSPECIFIED }, 0, 7, VE, "video_format" }, | ||
1207 | { "component", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_COMPONENT }, 0, 0, VE, "video_format" }, | ||
1208 | { "pal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_PAL }, 0, 0, VE, "video_format" }, | ||
1209 | { "ntsc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_NTSC }, 0, 0, VE, "video_format" }, | ||
1210 | { "secam", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_SECAM }, 0, 0, VE, "video_format" }, | ||
1211 | { "mac", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_MAC }, 0, 0, VE, "video_format" }, | ||
1212 | { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_UNSPECIFIED}, 0, 0, VE, "video_format" }, | ||
1213 | #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, "avctx.level" | ||
1214 | { LEVEL("high", 4) }, | ||
1215 | { LEVEL("high1440", 6) }, | ||
1216 | { LEVEL("main", 8) }, | ||
1217 | { LEVEL("low", 10) }, | ||
1218 | #undef LEVEL | ||
1219 | FF_MPV_COMMON_OPTS | ||
1220 | FF_MPV_COMMON_MOTION_EST_OPTS | ||
1221 | FF_MPEG2_PROFILE_OPTS | ||
1222 | { NULL }, | ||
1223 | }; | ||
1224 | |||
1225 | #define mpeg12_class(x) \ | ||
1226 | static const AVClass mpeg ## x ## _class = { \ | ||
1227 | .class_name = "mpeg" # x "video encoder", \ | ||
1228 | .item_name = av_default_item_name, \ | ||
1229 | .option = mpeg ## x ## _options, \ | ||
1230 | .version = LIBAVUTIL_VERSION_INT, \ | ||
1231 | }; | ||
1232 | |||
1233 | mpeg12_class(1) | ||
1234 | mpeg12_class(2) | ||
1235 | |||
1236 | const FFCodec ff_mpeg1video_encoder = { | ||
1237 | .p.name = "mpeg1video", | ||
1238 | CODEC_LONG_NAME("MPEG-1 video"), | ||
1239 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1240 | .p.id = AV_CODEC_ID_MPEG1VIDEO, | ||
1241 | .priv_data_size = sizeof(MPEG12EncContext), | ||
1242 | .init = encode_init, | ||
1243 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
1244 | .close = ff_mpv_encode_end, | ||
1245 | .p.supported_framerates = ff_mpeg12_frame_rate_tab + 1, | ||
1246 | .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, | ||
1247 | AV_PIX_FMT_NONE }, | ||
1248 | .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS | | ||
1249 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1250 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1251 | .p.priv_class = &mpeg1_class, | ||
1252 | }; | ||
1253 | |||
1254 | const FFCodec ff_mpeg2video_encoder = { | ||
1255 | .p.name = "mpeg2video", | ||
1256 | CODEC_LONG_NAME("MPEG-2 video"), | ||
1257 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
1258 | .p.id = AV_CODEC_ID_MPEG2VIDEO, | ||
1259 | .priv_data_size = sizeof(MPEG12EncContext), | ||
1260 | .init = encode_init, | ||
1261 | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), | ||
1262 | .close = ff_mpv_encode_end, | ||
1263 | .p.supported_framerates = ff_mpeg2_frame_rate_tab, | ||
1264 | .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, | ||
1265 | AV_PIX_FMT_YUV422P, | ||
1266 | AV_PIX_FMT_NONE }, | ||
1267 | .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS | | ||
1268 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
1269 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1270 | .p.priv_class = &mpeg2_class, | ||
1271 | }; | ||
1272 | #endif /* CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER */ | ||
1273 |