Revision d9c9259f

View differences:

libavcodec/Makefile
99 99
OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER)  += dpcm.o
100 100
OBJS-$(CONFIG_JPEGLS_ENCODER)          += jpeglsenc.o jpegls.o
101 101
OBJS-$(CONFIG_KMVC_DECODER)            += kmvc.o
102
OBJS-$(CONFIG_LJPEG_ENCODER)           += mjpegenc.o mjpeg.o mpegvideo.o
102
OBJS-$(CONFIG_LJPEG_ENCODER)           += ljpegenc.o mjpegenc.o mjpeg.o mpegvideo.o
103 103
OBJS-$(CONFIG_LOCO_DECODER)            += loco.o
104 104
OBJS-$(CONFIG_MACE3_DECODER)           += mace.o
105 105
OBJS-$(CONFIG_MACE6_DECODER)           += mace.o
libavcodec/ljpegenc.c
1
/*
2
 * lossless JPEG encoder
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
 * Copyright (c) 2003 Alex Beregszaszi
5
 * Copyright (c) 2003-2004 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
 * Support for external huffman table, various fixes (AVID workaround),
24
 * aspecting, new decode_frame mechanism and apple mjpeg-b support
25
 *                                  by Alex Beregszaszi
26
 */
27

  
28
/**
29
 * @file ljpegenc.c
30
 * lossless JPEG encoder.
31
 */
32

  
33
#include "avcodec.h"
34
#include "dsputil.h"
35
#include "mpegvideo.h"
36
#include "mjpeg.h"
37
#include "mjpegenc.h"
38

  
39

  
40
static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
41
    MpegEncContext * const s = avctx->priv_data;
42
    MJpegContext * const m = s->mjpeg_ctx;
43
    AVFrame *pict = data;
44
    const int width= s->width;
45
    const int height= s->height;
46
    AVFrame * const p= (AVFrame*)&s->current_picture;
47
    const int predictor= avctx->prediction_method+1;
48

  
49
    init_put_bits(&s->pb, buf, buf_size);
50

  
51
    *p = *pict;
52
    p->pict_type= FF_I_TYPE;
53
    p->key_frame= 1;
54

  
55
    mjpeg_picture_header(s);
56

  
57
    s->header_bits= put_bits_count(&s->pb);
58

  
59
    if(avctx->pix_fmt == PIX_FMT_RGB32){
60
        int x, y, i;
61
        const int linesize= p->linesize[0];
62
        uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
63
        int left[3], top[3], topleft[3];
64

  
65
        for(i=0; i<3; i++){
66
            buffer[0][i]= 1 << (9 - 1);
67
        }
68

  
69
        for(y = 0; y < height; y++) {
70
            const int modified_predictor= y ? predictor : 1;
71
            uint8_t *ptr = p->data[0] + (linesize * y);
72

  
73
            if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
74
                av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
75
                return -1;
76
            }
77

  
78
            for(i=0; i<3; i++){
79
                top[i]= left[i]= topleft[i]= buffer[0][i];
80
            }
81
            for(x = 0; x < width; x++) {
82
                buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
83
                buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
84
                buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
85

  
86
                for(i=0;i<3;i++) {
87
                    int pred, diff;
88

  
89
                    PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
90

  
91
                    topleft[i]= top[i];
92
                    top[i]= buffer[x+1][i];
93

  
94
                    left[i]= buffer[x][i];
95

  
96
                    diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
97

  
98
                    if(i==0)
99
                        mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
100
                    else
101
                        mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
102
                }
103
            }
104
        }
105
    }else{
106
        int mb_x, mb_y, i;
107
        const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
108
        const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
109

  
110
        for(mb_y = 0; mb_y < mb_height; mb_y++) {
111
            if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
112
                av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
113
                return -1;
114
            }
115
            for(mb_x = 0; mb_x < mb_width; mb_x++) {
116
                if(mb_x==0 || mb_y==0){
117
                    for(i=0;i<3;i++) {
118
                        uint8_t *ptr;
119
                        int x, y, h, v, linesize;
120
                        h = s->mjpeg_hsample[i];
121
                        v = s->mjpeg_vsample[i];
122
                        linesize= p->linesize[i];
123

  
124
                        for(y=0; y<v; y++){
125
                            for(x=0; x<h; x++){
126
                                int pred;
127

  
128
                                ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
129
                                if(y==0 && mb_y==0){
130
                                    if(x==0 && mb_x==0){
131
                                        pred= 128;
132
                                    }else{
133
                                        pred= ptr[-1];
134
                                    }
135
                                }else{
136
                                    if(x==0 && mb_x==0){
137
                                        pred= ptr[-linesize];
138
                                    }else{
139
                                        PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
140
                                    }
141
                                }
142

  
143
                                if(i==0)
144
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
145
                                else
146
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
147
                            }
148
                        }
149
                    }
150
                }else{
151
                    for(i=0;i<3;i++) {
152
                        uint8_t *ptr;
153
                        int x, y, h, v, linesize;
154
                        h = s->mjpeg_hsample[i];
155
                        v = s->mjpeg_vsample[i];
156
                        linesize= p->linesize[i];
157

  
158
                        for(y=0; y<v; y++){
159
                            for(x=0; x<h; x++){
160
                                int pred;
161

  
162
                                ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
163
//printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
164
                                PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
165

  
166
                                if(i==0)
167
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
168
                                else
169
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
170
                            }
171
                        }
172
                    }
173
                }
174
            }
175
        }
176
    }
177

  
178
    emms_c();
179

  
180
    mjpeg_picture_trailer(s);
181
    s->picture_number++;
182

  
183
    flush_put_bits(&s->pb);
184
    return pbBufPtr(&s->pb) - s->pb.buf;
185
//    return (put_bits_count(&f->pb)+7)/8;
186
}
187

  
188

  
189
AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
190
    "ljpeg",
191
    CODEC_TYPE_VIDEO,
192
    CODEC_ID_LJPEG,
193
    sizeof(MpegEncContext),
194
    MPV_encode_init,
195
    encode_picture_lossless,
196
    MPV_encode_end,
197
};
libavcodec/mjpegenc.c
37 37
#include "dsputil.h"
38 38
#include "mpegvideo.h"
39 39
#include "mjpeg.h"
40
#include "mjpegenc.h"
40 41

  
41 42
/* use two quantizer tables (one for luminance and one for chrominance) */
42 43
/* not yet working */
43 44
#undef TWOMATRIXES
44 45

  
45
typedef struct MJpegContext {
46
    uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing
47
    uint16_t huff_code_dc_luminance[12];
48
    uint8_t huff_size_dc_chrominance[12];
49
    uint16_t huff_code_dc_chrominance[12];
50

  
51
    uint8_t huff_size_ac_luminance[256];
52
    uint16_t huff_code_ac_luminance[256];
53
    uint8_t huff_size_ac_chrominance[256];
54
    uint16_t huff_code_ac_chrominance[256];
55
} MJpegContext;
56

  
57 46

  
58 47
int mjpeg_init(MpegEncContext *s)
59 48
{
......
365 354
    put_marker(&s->pb, EOI);
366 355
}
367 356

  
368
static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
357
void mjpeg_encode_dc(MpegEncContext *s, int val,
369 358
                                   uint8_t *huff_size, uint16_t *huff_code)
370 359
{
371 360
    int mant, nbits;
......
460 449
        encode_block(s, block[7], 7);
461 450
    }
462 451
}
463

  
464
static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
465
    MpegEncContext * const s = avctx->priv_data;
466
    MJpegContext * const m = s->mjpeg_ctx;
467
    AVFrame *pict = data;
468
    const int width= s->width;
469
    const int height= s->height;
470
    AVFrame * const p= (AVFrame*)&s->current_picture;
471
    const int predictor= avctx->prediction_method+1;
472

  
473
    init_put_bits(&s->pb, buf, buf_size);
474

  
475
    *p = *pict;
476
    p->pict_type= FF_I_TYPE;
477
    p->key_frame= 1;
478

  
479
    mjpeg_picture_header(s);
480

  
481
    s->header_bits= put_bits_count(&s->pb);
482

  
483
    if(avctx->pix_fmt == PIX_FMT_RGB32){
484
        int x, y, i;
485
        const int linesize= p->linesize[0];
486
        uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
487
        int left[3], top[3], topleft[3];
488

  
489
        for(i=0; i<3; i++){
490
            buffer[0][i]= 1 << (9 - 1);
491
        }
492

  
493
        for(y = 0; y < height; y++) {
494
            const int modified_predictor= y ? predictor : 1;
495
            uint8_t *ptr = p->data[0] + (linesize * y);
496

  
497
            if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
498
                av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
499
                return -1;
500
            }
501

  
502
            for(i=0; i<3; i++){
503
                top[i]= left[i]= topleft[i]= buffer[0][i];
504
            }
505
            for(x = 0; x < width; x++) {
506
                buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
507
                buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
508
                buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
509

  
510
                for(i=0;i<3;i++) {
511
                    int pred, diff;
512

  
513
                    PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
514

  
515
                    topleft[i]= top[i];
516
                    top[i]= buffer[x+1][i];
517

  
518
                    left[i]= buffer[x][i];
519

  
520
                    diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
521

  
522
                    if(i==0)
523
                        mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
524
                    else
525
                        mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
526
                }
527
            }
528
        }
529
    }else{
530
        int mb_x, mb_y, i;
531
        const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
532
        const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
533

  
534
        for(mb_y = 0; mb_y < mb_height; mb_y++) {
535
            if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
536
                av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
537
                return -1;
538
            }
539
            for(mb_x = 0; mb_x < mb_width; mb_x++) {
540
                if(mb_x==0 || mb_y==0){
541
                    for(i=0;i<3;i++) {
542
                        uint8_t *ptr;
543
                        int x, y, h, v, linesize;
544
                        h = s->mjpeg_hsample[i];
545
                        v = s->mjpeg_vsample[i];
546
                        linesize= p->linesize[i];
547

  
548
                        for(y=0; y<v; y++){
549
                            for(x=0; x<h; x++){
550
                                int pred;
551

  
552
                                ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
553
                                if(y==0 && mb_y==0){
554
                                    if(x==0 && mb_x==0){
555
                                        pred= 128;
556
                                    }else{
557
                                        pred= ptr[-1];
558
                                    }
559
                                }else{
560
                                    if(x==0 && mb_x==0){
561
                                        pred= ptr[-linesize];
562
                                    }else{
563
                                        PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
564
                                    }
565
                                }
566

  
567
                                if(i==0)
568
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
569
                                else
570
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
571
                            }
572
                        }
573
                    }
574
                }else{
575
                    for(i=0;i<3;i++) {
576
                        uint8_t *ptr;
577
                        int x, y, h, v, linesize;
578
                        h = s->mjpeg_hsample[i];
579
                        v = s->mjpeg_vsample[i];
580
                        linesize= p->linesize[i];
581

  
582
                        for(y=0; y<v; y++){
583
                            for(x=0; x<h; x++){
584
                                int pred;
585

  
586
                                ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
587
//printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
588
                                PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
589

  
590
                                if(i==0)
591
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
592
                                else
593
                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
594
                            }
595
                        }
596
                    }
597
                }
598
            }
599
        }
600
    }
601

  
602
    emms_c();
603

  
604
    mjpeg_picture_trailer(s);
605
    s->picture_number++;
606

  
607
    flush_put_bits(&s->pb);
608
    return pbBufPtr(&s->pb) - s->pb.buf;
609
//    return (put_bits_count(&f->pb)+7)/8;
610
}
611

  
612

  
613
AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
614
    "ljpeg",
615
    CODEC_TYPE_VIDEO,
616
    CODEC_ID_LJPEG,
617
    sizeof(MpegEncContext),
618
    MPV_encode_init,
619
    encode_picture_lossless,
620
    MPV_encode_end,
621
};
libavcodec/mjpegenc.h
1
/*
2
 * MJPEG encoder
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
 * Copyright (c) 2003 Alex Beregszaszi
5
 * Copyright (c) 2003-2004 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
 * Support for external huffman table, various fixes (AVID workaround),
24
 * aspecting, new decode_frame mechanism and apple mjpeg-b support
25
 *                                  by Alex Beregszaszi
26
 */
27

  
28
/**
29
 * @file mjpegenc.h
30
 * MJPEG encoder.
31
 */
32

  
33
#ifndef MJPEGENC_H
34
#define MJPEGENC_H
35

  
36
#include "dsputil.h"
37
#include "mpegvideo.h"
38

  
39
typedef struct MJpegContext {
40
    uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing
41
    uint16_t huff_code_dc_luminance[12];
42
    uint8_t huff_size_dc_chrominance[12];
43
    uint16_t huff_code_dc_chrominance[12];
44

  
45
    uint8_t huff_size_ac_luminance[256];
46
    uint16_t huff_code_ac_luminance[256];
47
    uint8_t huff_size_ac_chrominance[256];
48
    uint16_t huff_code_ac_chrominance[256];
49
} MJpegContext;
50

  
51
int mjpeg_init(MpegEncContext *s);
52
void mjpeg_close(MpegEncContext *s);
53
void mjpeg_picture_header(MpegEncContext *s);
54
void mjpeg_picture_trailer(MpegEncContext *s);
55
void ff_mjpeg_stuffing(PutBitContext *pbc);
56
void mjpeg_encode_dc(MpegEncContext *s, int val,
57
                     uint8_t *huff_size, uint16_t *huff_code);
58
void mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64]);
59

  
60
#endif /* MJPEGENC_H */
libavcodec/mpegvideo.c
30 30
#include "avcodec.h"
31 31
#include "dsputil.h"
32 32
#include "mpegvideo.h"
33
#include "mjpegenc.h"
33 34
#include "msmpeg4.h"
34 35
#include "faandct.h"
35 36
#include <limits.h>
libavcodec/mpegvideo.h
905 905
                       DCTELEM block[6][64],
906 906
                       int motion_x, int motion_y);
907 907

  
908
/* mjpeg.c */
909
int mjpeg_init(MpegEncContext *s);
910
void mjpeg_close(MpegEncContext *s);
911
void mjpeg_encode_mb(MpegEncContext *s,
912
                     DCTELEM block[6][64]);
913
void mjpeg_picture_header(MpegEncContext *s);
914
void mjpeg_picture_trailer(MpegEncContext *s);
915
void ff_mjpeg_stuffing(PutBitContext * pbc);
916

  
917 908
#endif /* AVCODEC_MPEGVIDEO_H */
918 909

  

Also available in: Unified diff