1 /*
2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * VC-1 and WMV3 decoder
27 */
28
40
41
42 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
43
44 typedef struct SpriteData {
45 /**
46 * Transform coefficients for both sprites in 16.16 fixed point format,
47 * in the order they appear in the bitstream:
48 * x scale
49 * rotation 1 (unused)
50 * x offset
51 * rotation 2 (unused)
52 * y scale
53 * y offset
54 * alpha
55 */
56 int coefs[2][7];
57
58 int effect_type, effect_flag;
59 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
60 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
61 } SpriteData;
62
64 {
66 }
67
68 static void vc1_sprite_parse_transform(
GetBitContext* gb,
int c[7])
69 {
70 c[1] = c[3] = 0;
71
73 case 0:
74 c[0] = 1 << 16;
75 c[2] = get_fp_val(gb);
76 c[4] = 1 << 16;
77 break;
78 case 1:
79 c[0] = c[4] = get_fp_val(gb);
80 c[2] = get_fp_val(gb);
81 break;
82 case 2:
83 c[0] = get_fp_val(gb);
84 c[2] = get_fp_val(gb);
85 c[4] = get_fp_val(gb);
86 break;
87 case 3:
88 c[0] = get_fp_val(gb);
89 c[1] = get_fp_val(gb);
90 c[2] = get_fp_val(gb);
91 c[3] = get_fp_val(gb);
92 c[4] = get_fp_val(gb);
93 break;
94 }
95 c[5] = get_fp_val(gb);
97 c[6] = get_fp_val(gb);
98 else
99 c[6] = 1 << 16;
100 }
101
103 {
105 int sprite, i;
106
107 for (sprite = 0; sprite <= v->
two_sprites; sprite++) {
108 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
109 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
112 for (i = 0; i < 7; i++)
114 sd->coefs[sprite][i] / (1<<16),
115 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
117 }
118
121 switch (sd->effect_pcount1 =
get_bits(gb, 4)) {
122 case 7:
123 vc1_sprite_parse_transform(gb, sd->effect_params1);
124 break;
125 case 14:
126 vc1_sprite_parse_transform(gb, sd->effect_params1);
127 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
128 break;
129 default:
130 for (i = 0; i < sd->effect_pcount1; i++)
131 sd->effect_params1[i] = get_fp_val(gb);
132 }
133 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
134 // effect 13 is simple alpha blending and matches the opacity above
136 for (i = 0; i < sd->effect_pcount1; i++)
138 sd->effect_params1[i] / (1 << 16),
139 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
141 }
142
143 sd->effect_pcount2 =
get_bits(gb, 16);
144 if (sd->effect_pcount2 > 10) {
147 } else if (sd->effect_pcount2) {
148 i = -1;
150 while (++i < sd->effect_pcount2) {
151 sd->effect_params2[i] = get_fp_val(gb);
153 sd->effect_params2[i] / (1 << 16),
154 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
155 }
157 }
158 }
161
166 }
169
170 return 0;
171 }
172
173 static void vc1_draw_sprites(
VC1Context *v, SpriteData* sd)
174 {
175 int i, plane, row, sprite;
176 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
178 int xoff[2], xadv[2], yoff[2], yadv[2],
alpha;
179 int ysub[2];
181
183 xoff[i] = av_clip(sd->coefs[i][2], 0, v->
sprite_width-1 << 16);
184 xadv[i] = sd->coefs[i][0];
187
188 yoff[i] = av_clip(sd->coefs[i][5], 0, v->
sprite_height-1 << 16);
190 }
191 alpha = av_clip_uint16(sd->coefs[1][6]);
192
195
199
200 for (sprite = 0; sprite <= v->
two_sprites; sprite++) {
203 int ycoord = yoff[sprite] + yadv[sprite] * row;
204 int yline = ycoord >> 16;
205 int next_line;
206 ysub[sprite] = ycoord & 0xFFFF;
207 if (sprite) {
210 }
212 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
213 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
214 if (ysub[sprite])
215 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
216 } else {
217 if (sr_cache[sprite][0] != yline) {
218 if (sr_cache[sprite][1] == yline) {
220 FFSWAP(
int, sr_cache[sprite][0], sr_cache[sprite][1]);
221 } else {
222 v->
vc1dsp.
sprite_h(v->
sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
223 sr_cache[sprite][0] = yline;
224 }
225 }
226 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
228 iplane + next_line, xoff[sprite],
229 xadv[sprite], width);
230 sr_cache[sprite][1] = yline + 1;
231 }
232 src_h[sprite][0] = v->
sr_rows[sprite][0];
233 src_h[sprite][1] = v->
sr_rows[sprite][1];
234 }
235 }
236
238 if (ysub[0]) {
240 } else {
241 memcpy(dst, src_h[0][0], width);
242 }
243 } else {
244 if (ysub[0] && ysub[1]) {
246 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
247 } else if (ysub[0]) {
249 src_h[1][0], alpha, width);
250 } else if (ysub[1]) {
252 src_h[0][0], (1<<16)-1-alpha, width);
253 } else {
255 }
256 }
257 }
258
259 if (!plane) {
261 xoff[i] >>= 1;
262 yoff[i] >>= 1;
263 }
264 }
265
266 }
267 }
268
269
271 {
275 SpriteData sd;
276
277 memset(&sd, 0, sizeof(sd));
278
279 ret = vc1_parse_sprites(v, gb, &sd);
280 if (ret < 0)
282
285 return -1;
286 }
287
291 }
292
296
297 vc1_draw_sprites(v, &sd);
298
299 return 0;
300 }
301
303 {
307 int plane, i;
308
309 /* Windows Media Image codecs have a convergence interval of two keyframes.
310 Since we can't enforce it, clear to black the missing sprite. This is
311 wrong but it looks better than doing nothing. */
312
317 plane ? 128 : 0, f->
linesize[plane]);
318 }
319
320 #endif
321
323 {
325 int i;
327
328 /* Allocate mb bitplanes */
335
346
347 /* allocate block type info in that way so it could be used with s->block_index[] */
352
353 /* allocate memory to store block level MV info */
362
363 /* Init coded blocks info */
365 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
366 // return -1;
367 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
368 // return -1;
369 }
370
372
374 for (i = 0; i < 4; i++)
377 }
378
393 }
394
395 return 0;
396 }
397
399 {
400 int i;
401 for (i = 0; i < 64; i++) {
402 #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
408 }
411 }
412
413 /** Initialize a VC1/WMV3 decoder
414 * @todo TODO: Handle VC-1 IDUs (Transport level?)
415 * @todo TODO: Decypher remaining bits in extra_data
416 */
418 {
423
424 /* save the container output size for WMImage */
427
429 return -1;
432 else
435
438 // ensure static VLC tables are initialized
443 // Hack to ensure the above functions will be called
444 // again once we know all necessary settings.
445 // That this is necessary might indicate a bug.
447
451
454
455 // looks like WMV3 has a sequence header stored in the extradata
456 // advanced sequence header may be before the first frame
457 // the last byte of the extradata is a version number, 1 for the
458 // samples we can decode
459
461
464
466 if (count > 0) {
469 } else if (count < 0) {
471 }
472 } else { // VC1/WVC1/WVP2
478 int seq_initialized = 0, ep_initialized = 0;
479
482 return -1;
483 }
484
486 start =
find_next_marker(start, end);
// in WVC1 extradata first byte is its size, but can be 0 in mkv
488 for (; next <
end; start = next) {
490 size = next - start - 4;
491 if (size <= 0)
492 continue;
500 }
501 seq_initialized = 1;
502 break;
507 }
508 ep_initialized = 1;
509 break;
510 }
511 }
513 if (!seq_initialized || !ep_initialized) {
515 return -1;
516 }
518 }
519
523
527
529
536
539
542 } else {
546 }
547
551
554
555 // prevent 16.16 overflows
560
564 }
565 }
566 return 0;
567 }
568
569 /** Close a VC1/WMV3 decoder
570 * @warning Initial try at using MpegEncContext stuff
571 */
573 {
575 int i;
576
578
579 for (i = 0; i < 4; i++)
600 return 0;
601 }
602
603
604 /** Decode a VC1/WMV3 frame
605 * @todo TODO: Handle VC-1 IDUs (Transport level?)
606 */
609 {
611 int buf_size = avpkt->
size, n_slices = 0, i,
ret;
617 int mb_height, n_slices1=-1;
618 struct {
621 int mby_start;
622 } *slices =
NULL, *tmp;
623
625
628
629 /* no supplementary picture */
631 /* special case for last picture */
636
637 *got_frame = 1;
638 }
639
640 return buf_size;
641 }
642
646 else
648 }
649
650 //for advanced profile we may need to parse and unescape data
652 int buf_size2 = 0;
654 if (!buf2)
656
657 if (
IS_MARKER(
AV_RB32(buf))) {
/* frame starts with marker and needs to be parsed */
660
662 for (start = buf, end = buf + buf_size; next <
end; start = next) {
664 size = next - start - 4;
665 if (size <= 0) continue;
672 break;
674 int buf_size3;
677 buf_start_second_field =
start;
679 if (!tmp)
680 goto err;
681 slices = tmp;
683 if (!slices[n_slices].buf)
684 goto err;
686 slices[n_slices].buf);
688 buf_size3 << 3);
689 /* assuming that the field marker is at the exact middle,
690 hope it's correct */
691 slices[n_slices].mby_start = s->
mb_height + 1 >> 1;
692 n_slices1 = n_slices - 1; // index of the last slice of the first field
693 n_slices++;
694 break;
695 }
700 break;
702 int buf_size3;
704 if (!tmp)
705 goto err;
706 slices = tmp;
708 if (!slices[n_slices].buf)
709 goto err;
711 slices[n_slices].buf);
713 buf_size3 << 3);
714 slices[n_slices].mby_start =
get_bits(&slices[n_slices].gb, 9);
715 n_slices++;
716 break;
717 }
718 }
719 }
720 }
else if (v->
interlace && ((buf[0] & 0xC0) == 0xC0)) {
/* WVC1 interlaced stores both fields divided by marker */
722 int buf_size3;
723
727 goto err;
728 } else { // found field marker, unescape second field
731 buf_start_second_field = divider;
733 if (!tmp)
734 goto err;
735 slices = tmp;
737 if (!slices[n_slices].buf)
738 goto err;
739 buf_size3 =
vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
741 buf_size3 << 3);
742 slices[n_slices].mby_start = s->
mb_height + 1 >> 1;
743 n_slices1 = n_slices - 1;
744 n_slices++;
745 }
747 } else {
749 }
751 } else
753
757 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
758 we're using the sprite compositor. These are intentionally kept separate
759 so you can get the raw sprites by using the wmv3 decoder for WMVP or
760 the vc1 one for WVP2 */
763 // switch AVCodecContext parameters to those of the sprites
766 } else {
767 goto image;
768 }
769 }
770 }
771
776 }
777
780 goto err;
783 goto err;
784 }
785
787
790 goto err;
793 }
794 }
795
796 // do parse frame header
801 goto err;
802 }
803 } else {
805 goto err;
806 }
807 }
809
812
816 goto err;
817 }
818
821 goto err;
822 }
823
824 // for skipping the frame
827
828 /* skip B-frames if we don't have reference frames */
832 }
837 }
838
842 else
844 }
845
847 goto err;
848 }
849
853
854 // process pulldown flags
856 // Pulldown flags are only valid when 'broadcast' has been set.
857 // So ticks_per_frame will be 2
859 // repeat field
862 // repeat frames
864 }
865
868
869 if ((CONFIG_VC1_VDPAU_DECODER)
871 if (v->
field_mode && buf_start_second_field) {
874 } else {
876 }
878 if (v->
field_mode && buf_start_second_field) {
879 // decode first field
881 if (avctx->
hwaccel->
start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
882 goto err;
883 if (avctx->
hwaccel->
decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
884 goto err;
886 goto err;
887
888 // decode second field
889 s->
gb = slices[n_slices1 + 1].gb;
895 goto err;
896 }
898
899 if (avctx->
hwaccel->
start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
900 goto err;
901 if (avctx->
hwaccel->
decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
902 goto err;
904 goto err;
905 } else {
908 goto err;
910 goto err;
912 goto err;
913 }
914 } else {
915 int header_ret = 0;
916
918
919 v->
bits = buf_size * 8;
927 }
929
931
932 for (i = 0; i <= n_slices; i++) {
933 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
936 "picture boundary (%d >= %d)\n", i,
937 slices[i - 1].mby_start, mb_height);
938 continue;
939 }
944 } else {
948 }
949 if (i) {
955 goto err;
956 continue;
957 }
963 goto err;
964 continue;
965 }
966 }
967 }
968 if (header_ret < 0)
969 continue;
970 s->
start_mb_y = (i == 0) ? 0 :
FFMAX(0, slices[i-1].mby_start % mb_height);
972 s->
end_mb_y = (i == n_slices ) ? mb_height :
FFMIN(mb_height, slices[i].mby_start % mb_height);
973 else {
974 if (i >= n_slices) {
976 continue;
977 }
978 s->
end_mb_y = (i <= n_slices1 + 1) ? mb_height :
FFMIN(mb_height, slices[i].mby_start % mb_height);
979 }
982 continue;
983 }
986 continue;
987 }
989 if (i != n_slices)
990 s->
gb = slices[i].gb;
991 }
1002 }
1003 }
1006 // if (get_bits_count(&s->gb) > buf_size * 8)
1007 // return -1;
1009 goto err;
1012 }
1013
1015
1017 image:
1022 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
1023 if (vc1_decode_sprites(v, &s->
gb))
1024 goto err;
1025 #endif
1027 goto err;
1028 *got_frame = 1;
1029 } else {
1032 goto err;
1034 *got_frame = 1;
1037 goto err;
1039 *got_frame = 1;
1040 }
1041 }
1042
1045 for (i = 0; i < n_slices; i++)
1048 return buf_size;
1049
1050 err:
1052 for (i = 0; i < n_slices; i++)
1055 return -1;
1056 }
1057
1058
1065 };
1066
1068 #if CONFIG_VC1_DXVA2_HWACCEL
1070 #endif
1071 #if CONFIG_VC1_VAAPI_HWACCEL
1073 #endif
1074 #if CONFIG_VC1_VDPAU_HWACCEL
1076 #endif
1079 };
1080
1092 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
1094 };
1095
1096 #if CONFIG_WMV3_DECODER
1108 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
1110 };
1111 #endif
1112
1113 #if CONFIG_WMV3_VDPAU_DECODER
1114 AVCodec ff_wmv3_vdpau_decoder = {
1115 .
name =
"wmv3_vdpau",
1126 };
1127 #endif
1128
1129 #if CONFIG_VC1_VDPAU_DECODER
1130 AVCodec ff_vc1_vdpau_decoder = {
1131 .
name =
"vc1_vdpau",
1142 };
1143 #endif
1144
1145 #if CONFIG_WMV3IMAGE_DECODER
1146 AVCodec ff_wmv3image_decoder = {
1147 .
name =
"wmv3image",
1156 .
flush = vc1_sprite_flush,
1160 },
1161 };
1162 #endif
1163
1164 #if CONFIG_VC1IMAGE_DECODER
1165 AVCodec ff_vc1image_decoder = {
1175 .
flush = vc1_sprite_flush,
1179 },
1180 };
1181 #endif