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
42
43
44 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
45
46 typedef struct SpriteData {
47 /**
48 * Transform coefficients for both sprites in 16.16 fixed point format,
49 * in the order they appear in the bitstream:
50 * x scale
51 * rotation 1 (unused)
52 * x offset
53 * rotation 2 (unused)
54 * y scale
55 * y offset
56 * alpha
57 */
58 int coefs[2][7];
59
60 int effect_type, effect_flag;
61 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
62 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
63 } SpriteData;
64
66 {
68 }
69
70 static void vc1_sprite_parse_transform(
GetBitContext* gb,
int c[7])
71 {
73
75 case 0:
77 c[2] = get_fp_val(gb);
79 break;
80 case 1:
81 c[0] =
c[4] = get_fp_val(gb);
82 c[2] = get_fp_val(gb);
83 break;
84 case 2:
85 c[0] = get_fp_val(gb);
86 c[2] = get_fp_val(gb);
87 c[4] = get_fp_val(gb);
88 break;
89 case 3:
90 c[0] = get_fp_val(gb);
91 c[1] = get_fp_val(gb);
92 c[2] = get_fp_val(gb);
93 c[3] = get_fp_val(gb);
94 c[4] = get_fp_val(gb);
95 break;
96 }
97 c[5] = get_fp_val(gb);
99 c[6] = get_fp_val(gb);
100 else
102 }
103
105 {
108
109 for (sprite = 0; sprite <= v->
two_sprites; sprite++) {
110 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
111 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
114 for (
i = 0;
i < 7;
i++)
116 sd->coefs[sprite][
i] / (1<<16),
117 (
abs(sd->coefs[sprite][
i]) & 0xFFFF) * 1000 / (1 << 16));
119 }
120
123 switch (sd->effect_pcount1 =
get_bits(gb, 4)) {
124 case 7:
125 vc1_sprite_parse_transform(gb, sd->effect_params1);
126 break;
127 case 14:
128 vc1_sprite_parse_transform(gb, sd->effect_params1);
129 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
130 break;
131 default:
132 for (
i = 0;
i < sd->effect_pcount1;
i++)
133 sd->effect_params1[
i] = get_fp_val(gb);
134 }
135 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
136 // effect 13 is simple alpha blending and matches the opacity above
138 for (
i = 0;
i < sd->effect_pcount1;
i++)
140 sd->effect_params1[
i] / (1 << 16),
141 (
abs(sd->effect_params1[
i]) & 0xFFFF) * 1000 / (1 << 16));
143 }
144
145 sd->effect_pcount2 =
get_bits(gb, 16);
146 if (sd->effect_pcount2 > 10) {
149 } else if (sd->effect_pcount2) {
152 while (++i < sd->effect_pcount2) {
153 sd->effect_params2[
i] = get_fp_val(gb);
155 sd->effect_params2[
i] / (1 << 16),
156 (
abs(sd->effect_params2[
i]) & 0xFFFF) * 1000 / (1 << 16));
157 }
159 }
160 }
163
168 }
171
172 return 0;
173 }
174
175 static void vc1_draw_sprites(
VC1Context *v, SpriteData* sd)
176 {
177 int i, plane, row, sprite;
178 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
179 uint8_t* src_h[2][2];
180 int xoff[2], xadv[2], yoff[2], yadv[2],
alpha;
181 int ysub[2];
183
186 xadv[
i] = sd->coefs[
i][0];
189
192 }
194
195 for (plane = 0; plane < (CONFIG_GRAY &&
s->avctx->flags &
AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) {
197
201
202 for (sprite = 0; sprite <= v->
two_sprites; sprite++) {
203 uint8_t *iplane =
s->current_picture.f->data[plane];
204 int iline =
s->current_picture.f->linesize[plane];
205 int ycoord = yoff[sprite] + yadv[sprite] * row;
206 int yline = ycoord >> 16;
207 int next_line;
208 ysub[sprite] = ycoord & 0xFFFF;
209 if (sprite) {
210 iplane =
s->last_picture.f->data[plane];
211 iline =
s->last_picture.f->linesize[plane];
212 }
214 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
215 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
216 if (ysub[sprite])
217 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
218 } else {
219 if (sr_cache[sprite][0] != yline) {
220 if (sr_cache[sprite][1] == yline) {
222 FFSWAP(
int, sr_cache[sprite][0], sr_cache[sprite][1]);
223 } else {
225 sr_cache[sprite][0] = yline;
226 }
227 }
228 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
230 iplane + next_line, xoff[sprite],
231 xadv[sprite],
width);
232 sr_cache[sprite][1] = yline + 1;
233 }
234 src_h[sprite][0] = v->
sr_rows[sprite][0];
235 src_h[sprite][1] = v->
sr_rows[sprite][1];
236 }
237 }
238
240 if (ysub[0]) {
242 } else {
243 memcpy(dst, src_h[0][0],
width);
244 }
245 } else {
246 if (ysub[0] && ysub[1]) {
248 src_h[1][0], src_h[1][1], ysub[1],
alpha,
width);
249 } else if (ysub[0]) {
252 } else if (ysub[1]) {
255 } else {
257 }
258 }
259 }
260
261 if (!plane) {
265 }
266 }
267
268 }
269 }
270
271
273 {
277 SpriteData sd;
278
279 memset(&sd, 0, sizeof(sd));
280
281 ret = vc1_parse_sprites(v, gb, &sd);
284
285 if (!
s->current_picture.f || !
s->current_picture.f->data[0]) {
288 }
289
290 if (v->
two_sprites && (!
s->last_picture_ptr || !
s->last_picture.f->data[0])) {
293 }
294
298
299 vc1_draw_sprites(v, &sd);
300
301 return 0;
302 }
303
305 {
310
311 /* Windows Media Image codecs have a convergence interval of two keyframes.
312 Since we can't enforce it, clear to black the missing sprite. This is
313 wrong but it looks better than doing nothing. */
314
316 for (plane = 0; plane < (CONFIG_GRAY &&
s->avctx->flags &
AV_CODEC_FLAG_GRAY ? 1 : 3); plane++)
318 memset(
f->data[plane] +
i *
f->linesize[plane],
319 plane ? 128 : 0,
f->linesize[plane]);
320 }
321
322 #endif
323
325 {
328 int mb_height =
FFALIGN(
s->mb_height, 2);
329
330 /* Allocate mb bitplanes */
340
359
360 /* allocate block type info in that way so it could be used with s->block_index[] */
367
368 /* allocate memory to store block level MV info */
373 v->
mv_f_base =
av_mallocz(2 * (
s->b8_stride * (mb_height * 2 + 1) +
s->mb_stride * (mb_height + 1) * 2));
377 v->
mv_f[1] = v->
mv_f[0] + (
s->b8_stride * (mb_height * 2 + 1) +
s->mb_stride * (mb_height + 1) * 2);
382 v->
mv_f_next[1] = v->
mv_f_next[0] + (
s->b8_stride * (mb_height * 2 + 1) +
s->mb_stride * (mb_height + 1) * 2);
383
385 for (
i = 0;
i < 4;
i++)
388 }
389
391 s->block,
s->block_last_index,
392 s->mb_width,
s->mb_height);
395
396 return 0;
397
401 }
402
404 {
406 for (
i = 0;
i < 64;
i++) {
407 #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
413 }
416 }
417
418 /** Initialize a VC1/WMV3 decoder
419 * @todo TODO: Handle VC-1 IDUs (Transport level?)
420 * @todo TODO: Decipher remaining bits in extra_data
421 */
423 {
428
429 /* save the container output size for WMImage */
432
436
438
440 int count = 0;
441
442 // looks like WMV3 has a sequence header stored in the extradata
443 // advanced sequence header may be before the first frame
444 // the last byte of the extradata is a version number, 1 for the
445 // samples we can decode
446
450
453
457 }
458
460 if (count > 0) {
463 } else if (count < 0) {
465 }
466 } else { // VC1/WVC1/WVP2
469 const uint8_t *next;
471 uint8_t *buf2 =
NULL;
472 int seq_initialized = 0, ep_initialized = 0;
473
477 }
478
480 if (!buf2)
482
483 start =
find_next_marker(start, end);
// in WVC1 extradata first byte is its size, but can be 0 in mkv
484 next = start;
485 for (; next < end; start = next) {
487 size = next - start - 4;
489 continue;
497 }
498 seq_initialized = 1;
499 break;
504 }
505 ep_initialized = 1;
506 break;
507 }
508 }
510 if (!seq_initialized || !ep_initialized) {
513 }
515 }
516
520
523 else {
527 }
528
529 // ensure static VLC tables are initialized
534 // Hack to ensure the above functions will be called
535 // again once we know all necessary settings.
536 // That this is necessary might indicate a bug.
538
542
544
551
554
557 } else {
561 }
562
566
569
570 // prevent 16.16 overflows
576 }
577
581 }
582 }
583 return 0;
584 }
585
586 /** Close a VC1/WMV3 decoder
587 * @warning Initial try at using MpegEncContext stuff
588 */
590 {
593
595
596 for (
i = 0;
i < 4;
i++)
615 return 0;
616 }
617
618
619 /** Decode a VC1/WMV3 frame
620 * @todo TODO: Handle VC-1 IDUs (Transport level?)
621 */
624 {
625 const uint8_t *buf = avpkt->
data;
626 int buf_size = avpkt->
size, n_slices = 0,
i,
ret;
630 uint8_t *buf2 =
NULL;
631 const uint8_t *buf_start = buf, *buf_start_second_field =
NULL;
632 int mb_height, n_slices1=-1;
633 struct {
634 uint8_t *buf;
636 int mby_start;
637 const uint8_t *rawbuf;
638 int raw_size;
640
642
645
646 /* no supplementary picture */
648 /* special case for last picture */
649 if (
s->low_delay == 0 &&
s->next_picture_ptr) {
652 s->next_picture_ptr =
NULL;
653
654 *got_frame = 1;
655 }
656
657 return buf_size;
658 }
659
660 //for advanced profile we may need to parse and unescape data
662 int buf_size2 = 0;
664 if (!buf2)
666
667 if (
IS_MARKER(
AV_RB32(buf))) {
/* frame starts with marker and needs to be parsed */
668 const uint8_t *start, *end, *next;
670
671 next = buf;
672 for (start = buf, end = buf + buf_size; next < end; start = next) {
674 size = next - start - 4;
675 if (
size <= 0)
continue;
679 buf_start = start;
681 break;
683 int buf_size3;
685 buf_start_second_field = start;
689 goto err;
690 }
693 if (!slices[n_slices].buf) {
695 goto err;
696 }
698 slices[n_slices].buf);
700 buf_size3 << 3);
701 slices[n_slices].mby_start = avctx->
coded_height + 31 >> 5;
702 slices[n_slices].rawbuf = start;
703 slices[n_slices].raw_size =
size + 4;
704 n_slices1 = n_slices - 1; // index of the last slice of the first field
705 n_slices++;
706 break;
707 }
712 break;
714 int buf_size3;
718 goto err;
719 }
722 if (!slices[n_slices].buf) {
724 goto err;
725 }
727 slices[n_slices].buf);
729 buf_size3 << 3);
730 slices[n_slices].mby_start =
get_bits(&slices[n_slices].gb, 9);
731 slices[n_slices].rawbuf = start;
732 slices[n_slices].raw_size =
size + 4;
733 n_slices++;
734 break;
735 }
736 }
737 }
738 }
else if (v->
interlace && ((buf[0] & 0xC0) == 0xC0)) {
/* WVC1 interlaced stores both fields divided by marker */
739 const uint8_t *divider;
740 int buf_size3;
741
746 goto err;
747 } else { // found field marker, unescape second field
749 buf_start_second_field = divider;
753 goto err;
754 }
757 if (!slices[n_slices].buf) {
759 goto err;
760 }
761 buf_size3 =
vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
763 buf_size3 << 3);
764 slices[n_slices].mby_start =
s->mb_height + 1 >> 1;
765 slices[n_slices].rawbuf = divider;
766 slices[n_slices].raw_size = buf + buf_size - divider;
767 n_slices1 = n_slices - 1;
768 n_slices++;
769 }
771 } else {
773 }
775 } else{
779 }
780
784 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
785 we're using the sprite compositor. These are intentionally kept separate
786 so you can get the raw sprites by using the wmv3 decoder for WMVP or
787 the vc1 one for WVP2 */
790 // switch AVCodecContext parameters to those of the sprites
793 } else {
794 goto image;
795 }
796 }
797 }
798
799 if (
s->context_initialized &&
803 }
804
805 if (!
s->context_initialized) {
807 goto err;
810 goto err;
811 }
812
814
818 goto err;
819 }
822 }
823 }
824
825 // do parse frame header
830 goto err;
831 }
832 } else {
834 goto err;
835 }
836 }
838
841
846 goto err;
847 }
852 goto err;
853 }
857 goto err;
858 }
859
860 // for skipping the frame
861 s->current_picture.f->pict_type =
s->pict_type;
863
864 /* skip B-frames if we don't have reference frames */
867 goto end;
868 }
872 goto end;
873 }
874
875 if (
s->next_p_frame_damaged) {
877 goto end;
878 else
879 s->next_p_frame_damaged = 0;
880 }
881
883 goto err;
884 }
885
889
890 // process pulldown flags
891 s->current_picture_ptr->f->repeat_pict = 0;
892 // Pulldown flags are only valid when 'broadcast' has been set.
893 // So ticks_per_frame will be 2
895 // repeat field
896 s->current_picture_ptr->f->repeat_pict = 1;
898 // repeat frames
899 s->current_picture_ptr->f->repeat_pict = v->
rptfrm * 2;
900 }
901
902 s->me.qpel_put =
s->qdsp.put_qpel_pixels_tab;
903 s->me.qpel_avg =
s->qdsp.avg_qpel_pixels_tab;
904
907 if (v->
field_mode && buf_start_second_field) {
908 // decode first field
911 goto err;
912
913 if (n_slices1 == -1) {
914 // no slices, decode the field as-is
916 goto err;
917 } else {
919 goto err;
920
921 for (
i = 0 ;
i < n_slices1 + 1;
i++) {
922 s->gb = slices[
i].gb;
923 s->mb_y = slices[
i].mby_start;
924
931 goto err;
932 continue;
933 }
934 }
935
937 goto err;
938 }
939 }
940
942 goto err;
943
944 // decode second field
945 s->gb = slices[n_slices1 + 1].gb;
946 s->mb_y = slices[n_slices1 + 1].mby_start;
953 goto err;
954 }
956
957 if ((
ret = avctx->
hwaccel->
start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
958 goto err;
959
960 if (n_slices - n_slices1 == 2) {
961 // no slices, decode the field as-is
962 if ((
ret = avctx->
hwaccel->
decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
963 goto err;
964 } else {
965 if ((
ret = avctx->
hwaccel->
decode_slice(avctx, buf_start_second_field, slices[n_slices1 + 2].rawbuf - buf_start_second_field)) < 0)
966 goto err;
967
968 for (
i = n_slices1 + 2;
i < n_slices;
i++) {
969 s->gb = slices[
i].gb;
970 s->mb_y = slices[
i].mby_start;
971
978 goto err;
979 continue;
980 }
981 }
982
984 goto err;
985 }
986 }
987
989 goto err;
990 } else {
993 goto err;
994
995 if (n_slices == 0) {
996 // no slices, decode the frame as-is
998 goto err;
999 } else {
1000 // decode the frame part as the first slice
1002 goto err;
1003
1004 // and process the slices as additional slices afterwards
1005 for (
i = 0 ;
i < n_slices;
i++) {
1006 s->gb = slices[
i].gb;
1007 s->mb_y = slices[
i].mby_start;
1008
1015 goto err;
1016 continue;
1017 }
1018 }
1019
1021 goto err;
1022 }
1023 }
1025 goto err;
1026 }
1027 } else {
1028 int header_ret = 0;
1029
1031
1034 s->current_picture.f->linesize[0] <<= 1;
1035 s->current_picture.f->linesize[1] <<= 1;
1036 s->current_picture.f->linesize[2] <<= 1;
1038 s->uvlinesize <<= 1;
1039 }
1041
1043
1044 for (
i = 0;
i <= n_slices;
i++) {
1045 if (
i > 0 && slices[
i - 1].mby_start >= mb_height) {
1048 "picture boundary (%d >= %d)\n",
i,
1049 slices[
i - 1].mby_start, mb_height);
1050 continue;
1051 }
1055 v->
mb_off =
s->mb_stride *
s->mb_height >> 1;
1056 } else {
1060 }
1068 goto err;
1069 continue;
1070 }
1077 goto err;
1078 continue;
1079 }
1080 }
1081 }
1082 if (header_ret < 0)
1083 continue;
1084 s->start_mb_y = (
i == 0) ? 0 :
FFMAX(0, slices[
i-1].mby_start % mb_height);
1086 s->end_mb_y = (
i == n_slices ) ? mb_height :
FFMIN(mb_height, slices[
i].mby_start % mb_height);
1087 else {
1088 if (
i >= n_slices) {
1090 continue;
1091 }
1092 s->end_mb_y = (
i == n_slices1 + 1) ? mb_height :
FFMIN(mb_height, slices[
i].mby_start % mb_height);
1093 }
1094 if (
s->end_mb_y <=
s->start_mb_y) {
1096 continue;
1097 }
1102 continue;
1103 }
1105 if (
i != n_slices) {
1106 s->gb = slices[
i].gb;
1107 }
1108 }
1111 s->current_picture.f->linesize[0] >>= 1;
1112 s->current_picture.f->linesize[1] >>= 1;
1113 s->current_picture.f->linesize[2] >>= 1;
1115 s->uvlinesize >>= 1;
1119 }
1120 }
1121 ff_dlog(
s->avctx,
"Consumed %i/%i bits\n",
1123 // if (get_bits_count(&s->gb) > buf_size * 8)
1124 // return -1;
1127 goto err;
1128 }
1133 }
1134
1136
1138 image:
1142 goto end;
1146 goto err;
1147 }
1148 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
1149 if ((
ret = vc1_decode_sprites(v, &
s->gb)) < 0)
1150 goto err;
1151 #endif
1153 goto err;
1154 *got_frame = 1;
1155 } else {
1158 goto err;
1161 *got_frame = 1;
1162 }
else if (
s->last_picture_ptr) {
1164 goto err;
1167 *got_frame = 1;
1168 }
1169 }
1170
1171 end:
1173 for (
i = 0;
i < n_slices;
i++)
1176 return buf_size;
1177
1178 err:
1180 for (
i = 0;
i < n_slices;
i++)
1184 }
1185
1186
1188 #if CONFIG_VC1_DXVA2_HWACCEL
1190 #endif
1191 #if CONFIG_VC1_D3D11VA_HWACCEL
1194 #endif
1195 #if CONFIG_VC1_NVDEC_HWACCEL
1197 #endif
1198 #if CONFIG_VC1_VAAPI_HWACCEL
1200 #endif
1201 #if CONFIG_VC1_VDPAU_HWACCEL
1203 #endif
1206 };
1207
1221 #if CONFIG_VC1_DXVA2_HWACCEL
1223 #endif
1224 #if CONFIG_VC1_D3D11VA_HWACCEL
1226 #endif
1227 #if CONFIG_VC1_D3D11VA2_HWACCEL
1229 #endif
1230 #if CONFIG_VC1_NVDEC_HWACCEL
1232 #endif
1233 #if CONFIG_VC1_VAAPI_HWACCEL
1235 #endif
1236 #if CONFIG_VC1_VDPAU_HWACCEL
1238 #endif
1240 },
1242 };
1243
1244 #if CONFIG_WMV3_DECODER
1258 #if CONFIG_WMV3_DXVA2_HWACCEL
1260 #endif
1261 #if CONFIG_WMV3_D3D11VA_HWACCEL
1263 #endif
1264 #if CONFIG_WMV3_D3D11VA2_HWACCEL
1266 #endif
1267 #if CONFIG_WMV3_NVDEC_HWACCEL
1269 #endif
1270 #if CONFIG_WMV3_VAAPI_HWACCEL
1272 #endif
1273 #if CONFIG_WMV3_VDPAU_HWACCEL
1275 #endif
1277 },
1279 };
1280 #endif
1281
1282 #if CONFIG_WMV3IMAGE_DECODER
1284 .
name =
"wmv3image",
1293 .
flush = vc1_sprite_flush,
1297 },
1298 };
1299 #endif
1300
1301 #if CONFIG_VC1IMAGE_DECODER
1312 .
flush = vc1_sprite_flush,
1316 },
1317 };
1318 #endif