3
3
* Copyright (c) 2003 Michael Niedermayer
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
25
27
#include "avcodec.h"
26
28
#include "dsputil.h"
27
29
#include "mpegvideo.h"
124
DCTELEM __align8 block[6][64];
126
DECLARE_ALIGNED_8(DCTELEM, block[6][64]);
125
127
uint8_t *bitstream_buffer;
126
int bitstream_buffer_size;
128
unsigned int bitstream_buffer_size;
127
129
CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
222
224
static void init_vlcs(FourXContext *f){
230
init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7,
231
&block_type_tab[i][0][1], 2, 1,
232
&block_type_tab[i][0][0], 2, 1);
228
init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7,
229
&block_type_tab[i][0][1], 2, 1,
230
&block_type_tab[i][0][0], 2, 1, 1);
328
325
uint16_t *src= (uint16_t*)f->last_picture.data[0];
329
326
uint16_t *dst= (uint16_t*)f->current_picture.data[0];
330
327
const int stride= f->current_picture.linesize[0]>>1;
331
const int bitstream_size= get32(buf+8);
332
const int bytestream_size= get32(buf+16);
333
const int wordstream_size= get32(buf+12);
335
if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length)
336
printf("lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
328
const unsigned int bitstream_size= get32(buf+8);
329
const unsigned int bytestream_size= get32(buf+16);
330
const unsigned int wordstream_size= get32(buf+12);
332
if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length
333
|| bitstream_size > (1<<26)
334
|| bytestream_size > (1<<26)
335
|| wordstream_size > (1<<26)
337
av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
337
338
bitstream_size+ bytestream_size+ wordstream_size - length);
339
342
f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
340
343
f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + 20), bitstream_size/4);
341
344
init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
343
346
f->wordstream= (uint16_t*)(buf + 20 + bitstream_size);
344
347
f->bytestream= buf + 20 + bitstream_size + wordstream_size;
348
351
for(y=0; y<height; y+=8){
349
352
for(x=0; x<width; x+=8){
350
353
decode_p_block(f, dst + x, src + x, 3, 3, stride);
356
359
if(bitstream_size != (get_bits_count(&f->gb)+31)/32*4)
357
printf(" %d %d %d bytes left\n",
358
bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
360
av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
361
bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
359
362
bytestream_size - (f->bytestream - (buf + 20 + bitstream_size + wordstream_size)),
360
363
wordstream_size - (((uint8_t*)f->wordstream) - (buf + 20 + bitstream_size))
484
492
for(i=start; i<=end; i++){
485
493
frequency[i]= *ptr++;
486
// printf("%d %d %d\n", start, end, frequency[i]);
489
496
if(start==0) break;
493
500
frequency[256]=1;
495
while((ptr - buf)&3) ptr++; // 4byte align
502
while((ptr - buf)&3) ptr++; // 4byte align
497
// for(j=0; j<16; j++)
498
// printf("%2X", ptr[j]);
500
504
for(j=257; j<512; j++){
501
505
int min_freq[2]= {256*256, 256*256};
502
506
int smallest[2]= {0, 0};
530
534
for(node= j; up[node] != -1; node= up[node]){
531
535
bits += flag[node]<<len;
533
if(len > 31) printf("vlc length overflow\n"); //can this happen at all ?
537
if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n"); //can this happen at all ?
536
540
bits_tab[j]= bits;
540
init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
544
init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
550
554
const int height= f->avctx->height;
551
555
uint16_t *dst= (uint16_t*)f->current_picture.data[0];
552
556
const int stride= f->current_picture.linesize[0]>>1;
553
const int bitstream_size= get32(buf);
557
const unsigned int bitstream_size= get32(buf);
554
558
const int token_count __attribute__((unused)) = get32(buf + bitstream_size + 8);
555
int prestream_size= 4*get32(buf + bitstream_size + 4);
559
unsigned int prestream_size= 4*get32(buf + bitstream_size + 4);
556
560
uint8_t *prestream= buf + bitstream_size + 12;
558
if(prestream_size + bitstream_size + 12 != length)
559
fprintf(stderr, "size missmatch %d %d %d\n", prestream_size, bitstream_size, length);
562
if(prestream_size + bitstream_size + 12 != length
563
|| bitstream_size > (1<<26)
564
|| prestream_size > (1<<26)){
565
av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n", prestream_size, bitstream_size, length);
561
569
prestream= read_huffman_tables(f, prestream);
563
571
init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
578
586
idct_put(f, x, y);
583
591
if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
584
printf("end missmatch\n");
592
av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
589
static int decode_frame(AVCodecContext *avctx,
597
static int decode_frame(AVCodecContext *avctx,
590
598
void *data, int *data_size,
591
599
uint8_t *buf, int buf_size)
595
603
AVFrame *p, temp;
596
604
int i, frame_4cc, frame_size;
600
/* special case for last picture */
605
606
frame_4cc= get32(buf);
606
if(buf_size != get32(buf+4)+8){
607
fprintf(stderr, "size missmatch %d %d\n", buf_size, get32(buf+4));
607
if(buf_size != get32(buf+4)+8 || buf_size < 20){
608
av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", buf_size, get32(buf+4));
610
611
if(frame_4cc == ff_get_fourcc("cfrm")){
617
618
for(i=0; i<CFRAME_BUFFER_COUNT; i++){
618
619
if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
619
printf("lost c frame %d\n", f->cfrm[i].id);
620
av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
622
623
for(i=0; i<CFRAME_BUFFER_COUNT; i++){
623
624
if(f->cfrm[i].id == id) break;
624
625
if(f->cfrm[i].size == 0 ) free_index= i;
629
630
f->cfrm[i].id= id;
631
632
cfrm= &f->cfrm[i];
633
634
cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
635
if(!cfrm->data){ //explicit check needed as memcpy below might not catch a NULL
636
av_log(f->avctx, AV_LOG_ERROR, "realloc falure");
635
640
memcpy(cfrm->data + cfrm->size, buf+20, data_size);
636
641
cfrm->size += data_size;
638
643
if(cfrm->size >= whole_size){
640
645
frame_size= cfrm->size;
642
647
if(id != avctx->frame_number){
643
printf("cframe id missmatch %d %d\n", id, avctx->frame_number);
648
av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n", id, avctx->frame_number);
646
651
cfrm->size= cfrm->id= 0;
647
652
frame_4cc= ff_get_fourcc("pfrm");
679
684
if(decode_p_frame(f, buf, frame_size) < 0)
681
686
}else if(frame_4cc == ff_get_fourcc("snd_")){
682
printf("ignoring snd_ chunk length:%d\n", buf_size);
687
av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
684
printf("ignoring unknown chunk length:%d\n", buf_size);
689
av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
689
printf("%2X %c ", buf[i], clip(buf[i],16,126));
693
692
p->key_frame= p->pict_type == I_TYPE;
696
695
*data_size = sizeof(AVPicture);