1 /*
2 * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
3 * Copyright (c) 2012 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
25 */
26
32
33 #define HEADER_SIZE 27
34
35 #define MODEL2_SCALE 13
36 #define MODEL_SCALE 15
37 #define MODEL256_SEC_SCALE 9
38
44
45 typedef struct Model {
51
59
60 #define RAC_BOTTOM 0x01000000
63
67
74 };
75
80
85
91
102
108
112
120
124
125
127 {
134 }
135
137 {
138 unsigned scale;
139
140 if (!bit)
144 return;
145
152 }
160 }
161
163 {
164 int i, sum = 0;
165 unsigned scale;
166
170 return;
172
178 }
179 }
182 m->
freqs[i] = sum * scale >> 16;
184 }
185
190 }
191
193 {
194 int i;
195
197 for (i = 0; i < m->
num_syms - 1; i++)
200
206 }
207
209 {
212
214 }
215
217 {
218 int i, sum = 0;
219 unsigned scale;
220 int send, sidx = 1;
221
225 return;
227
230 for (i = 0; i < 256; i++) {
233 }
234 }
237 for (i = 0; i < 256; i++) {
238 m->
freqs[i] = sum * scale >> 16;
241 while (sidx <= send)
243 }
244 while (sidx < m->sec_size)
246
251 }
252
254 {
255 int i;
256
257 for (i = 0; i < 255; i++)
260
267 }
268
270 {
273
275 }
276
278 {
279 int i;
280
284 for (i = 0; i <
FFMIN(size, 4); i++)
286 c->
range = 0xFFFFFFFF;
288 }
289
291 {
292 for (;;) {
297 }
else if (!c->
low) {
300 }
302 return;
303 }
304 }
305
307 {
308 int bit;
309
311
313 if (bit)
315
318
319 return bit;
320 }
321
323 {
325
329
332
334 }
335
337 {
338 int bit, helper;
339
341 bit = (c->
low >= helper);
342 if (bit) {
345 } else {
347 }
348
351
353
354 return bit;
355 }
356
358 {
361 unsigned prob, prob2, helper;
362
363 prob = 0;
366 val = 0;
369 do {
371 if (helper <= c->low) {
373 prob = helper;
374 } else {
376 prob2 = helper;
377 }
378 end = (end2 +
val) >> 1;
379 } while (end != val);
381 c->
range = prob2 - prob;
384
386
388 }
389
391 {
394 int ssym;
395 unsigned prob, prob2, helper;
396
399
403
404 end = start = m->
secondary[ssym + 1] + 1;
405 while (end > val + 1) {
406 ssym = (end +
val) >> 1;
407 if (m->
freqs[ssym] <= helper) {
409 val = ssym;
410 } else {
411 end = (end +
val) >> 1;
412 start = ssym;
413 }
414 }
416 if (val != 255)
418
420 c->
range = prob2 - prob;
423
425
427 }
428
430 {
432
434 }
435
437 {
439
441 if (val) {
443 if (val > 1) {
444 val--;
446 }
447 if (!sign)
449 }
450
452 }
453
456 {
457 int i;
458
460
461 for (i = 0; i < block_size; i++, dst +=
stride)
462 memset(dst, fc->
fill_val, block_size);
463 }
464
467 {
468 int i, j;
469 int vec_size;
470 int vec[4];
471 int prev_line[16];
473
475 for (i = 0; i < vec_size; i++)
477 for (; i < 4; i++)
478 vec[i] = 0;
479 memset(prev_line, 0, sizeof(prev_line));
480
481 for (j = 0; j < block_size; j++) {
482 A = 0;
483 B = 0;
484 for (i = 0; i < block_size; i++) {
486 B = prev_line[i];
488
490 if (A < 4)
492 else
494 }
496 }
497 }
498
500 int bx, int by)
501 {
502 int skip,
val, sign, pos = 1, zz_pos,
dc;
504
505 memset(block, 0, sizeof(*block) * 64);
506
508 if (by) {
509 if (bx) {
510 int l, tl, t;
511
515
517 dc += l;
518 else
519 dc += t;
520 } else {
522 }
523 } else if (bx) {
525 }
527 block[0] = dc * bc->
qmat[0];
528
529 while (pos < 64) {
531 if (!val)
532 return 0;
533 if (val == 0xF0) {
534 pos += 16;
535 continue;
536 }
537 skip = val >> 4;
538 val = val & 0xF;
539 if (!val)
540 return -1;
541 pos += skip;
542 if (pos >= 64)
543 return -1;
544
546 if (val > 1) {
547 val--;
549 }
550 if (!sign)
552
554 block[zz_pos] = val * bc->
qmat[zz_pos];
555 pos++;
556 }
557
558 return pos == 64 ? 0 : -1;
559 }
560
563 int *
block,
int mb_x,
int mb_y)
564 {
565 int i, j;
566 int bx, by;
567 int nblocks = block_size >> 3;
568
569 bx = mb_x * nblocks;
570 by = mb_y * nblocks;
571
572 for (j = 0; j < nblocks; j++) {
573 for (i = 0; i < nblocks; i++) {
574 if (
decode_dct(c, bc, block, bx + i, by + j)) {
576 return;
577 }
579 }
581 }
582 }
583
586 int block_size,
int *
block)
587 {
588 const int hsize = block_size >> 1;
590 int i, j;
591
592 for (j = 0; j < block_size; j++) {
593 for (i = 0; i < block_size; i++) {
594 if (i < hsize && j < hsize)
596 else
598 block[i] *= hc->
scale;
599 }
600 block += block_size;
601 }
602 block -= block_size * block_size;
603
604 for (j = 0; j < hsize; j++) {
605 for (i = 0; i < hsize; i++) {
606 A = block[i];
607 B = block[i + hsize];
608 C = block[i + hsize * block_size];
609 D = block[i + hsize * block_size + hsize];
610
615 dst[i * 2] = av_clip_uint8(t1 - t2);
616 dst[i * 2 +
stride] = av_clip_uint8(t1 + t2);
617 dst[i * 2 + 1] = av_clip_uint8(t3 - t4);
618 dst[i * 2 + 1 +
stride] = av_clip_uint8(t3 + t4);
619 }
620 block += block_size;
621 dst += stride * 2;
622 }
623 }
624
626 {
627 int i, j;
628
629 for (i = 0; i < 3; i++) {
631 for (j = 0; j < 5; j++)
638 for (j = 0; j < 125; j++)
643 }
654 }
657 }
658 }
659
661 {
662 int i, j;
663
664 for (i = 0; i < 3; i++) {
665 for (j = 0; j < 5; j++)
671 for (j = 0; j < 125; j++)
677 }
678 }
679
682 {
684 int buf_size = avpkt->
size;
689 int dec_width, dec_height, dec_x, dec_y, quality, keyframe;
690 int x, y, i, mb_width, mb_height, blk_size, btype;
691 int ret;
692
695 "Frame should have at least %d bytes, got %d instead\n",
698 }
699
701 keyframe = bytestream2_get_be32(&gb);
702 if (keyframe & ~0x301) {
705 }
706 keyframe = !(keyframe & 1);
708 dec_x = bytestream2_get_be16(&gb);
709 dec_y = bytestream2_get_be16(&gb);
710 dec_width = bytestream2_get_be16(&gb);
711 dec_height = bytestream2_get_be16(&gb);
712
713 if (dec_x + dec_width > avctx->
width ||
714 dec_y + dec_height > avctx->
height ||
715 (dec_width | dec_height) & 0xF) {
717 dec_width, dec_height, dec_x, dec_y);
719 }
721 quality = bytestream2_get_byte(&gb);
722 if (quality < 1 || quality > 100) {
725 }
727
731 }
733 return buf_size;
735
737 return ret;
742 return ret;
743 *got_frame = 1;
744
745 return buf_size;
746 }
747
749
751
752 mb_width = dec_width >> 4;
753 mb_height = dec_height >> 4;
757 for (y = 0; y < mb_height; y++) {
758 for (x = 0; x < mb_width; x++) {
759 for (i = 0; i < 3; i++) {
760 blk_size = 8 << !i;
761
763 switch (btype) {
766 dst[i] + x * blk_size,
768 break;
771 dst[i] + x * blk_size,
773 break;
776 dst[i] + x * blk_size,
779 break;
782 dst[i] + x * blk_size,
785 break;
786 }
789 x, y);
792 }
793 }
794 }
798 }
799
801 return ret;
802
803 *got_frame = 1;
804
805 return buf_size;
806 }
807
809 {
811 int i;
812
814 for (i = 0; i < 3; i++)
816
817 return 0;
818 }
819
821 {
823 int i;
824
826
827 if ((avctx->
width & 0xF) || (avctx->
height & 0xF)) {
829 "Image dimensions should be a multiple of 16.\n");
831 }
832
834 for (i = 0; i < 3; i++) {
835 int b_width = avctx->
width >> (2 + !!i);
836 int b_height = avctx->
height >> (2 + !!i);
840 b_width * b_height);
844 while (i >= 0) {
846 i--;
847 }
849 }
850 }
851
856 }
857
859
861
862 return 0;
863 }
864
875 };
const char const char void * val
void ff_mss34_gen_quant_mat(uint16_t *qmat, int quality, int luma)
Generate quantisation matrix for given quality.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic, uint8_t *dst, ptrdiff_t stride, int block_size)
static int decode_block_type(RangeCoder *c, BlockTypeContext *bt)
This structure describes decoded (raw) audio or video data.
#define MODEL256_SEC_SCALE
ptrdiff_t const GLvoid * data
static void model2_reset(Model2 *m)
static av_cold int init(AVCodecContext *avctx)
ImageBlockCoder image_coder[3]
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
int16_t weights[MODEL_MAX_SYMS+1]
static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc, uint8_t *dst, ptrdiff_t stride, int block_size, int *block)
static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static av_cold int end(AVCodecContext *avctx)
static void rac_normalise(RangeCoder *c)
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block, int bx, int by)
#define u(width, name, range_min, range_max)
static void reset_coders(MSS3Context *ctx, int quality)
static void rac_init(RangeCoder *c, const uint8_t *src, int size)
static void model256_update(Model256 *m, int val)
static av_cold void model_init(Model *m, int num_syms)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
DCTBlockCoder dct_coder[3]
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
const char * name
Name of the codec implementation.
static av_cold void model256_init(Model256 *m)
HaarBlockCoder haar_coder[3]
void ff_mss34_dct_put(uint8_t *dst, ptrdiff_t stride, int *block)
Transform and output DCT block.
static const uint16_t fc[]
enum AVPictureType pict_type
Picture type of the frame.
static av_cold int mss3_decode_init(AVCodecContext *avctx)
int width
picture width / height.
static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc, uint8_t *dst, ptrdiff_t stride, int block_size, int *block, int mb_x, int mb_y)
static int rac_get_bits(RangeCoder *c, int nbits)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static void model_reset(Model *m)
static int rac_get_model256_sym(RangeCoder *c, Model256 *m)
Libavcodec external API header.
static int rac_get_model_sym(RangeCoder *c, Model *m)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
static void model_update(Model *m, int val)
static av_cold void init_coders(MSS3Context *ctx)
static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc, uint8_t *dst, ptrdiff_t stride, int block_size)
static int rac_get_bit(RangeCoder *c)
static void model256_reset(Model256 *m)
const uint8_t ff_zigzag_direct[64]
FillBlockCoder fill_coder[3]
static av_cold int mss3_decode_end(AVCodecContext *avctx)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void model2_update(Model2 *m, int bit)
GLint GLenum GLboolean GLsizei stride
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
static int decode_coeff(RangeCoder *c, Model *m)
int key_frame
1 -> keyframe, 0-> not
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
BlockTypeContext btype[3]
static int rac_get_model2_sym(RangeCoder *c, Model2 *m)
This structure stores compressed data.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.