Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mjpegenc.c @ 944721c1

History | View | Annotate | Download (19.2 KB)

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.c
30
 * MJPEG encoder.
31
 */
32

    
33
//#define DEBUG
34
#include <assert.h>
35

    
36
#include "avcodec.h"
37
#include "dsputil.h"
38
#include "mpegvideo.h"
39
#include "mjpeg.h"
40

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

    
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

    
58
int mjpeg_init(MpegEncContext *s)
59
{
60
    MJpegContext *m;
61

    
62
    m = av_malloc(sizeof(MJpegContext));
63
    if (!m)
64
        return -1;
65

    
66
    s->min_qcoeff=-1023;
67
    s->max_qcoeff= 1023;
68

    
69
    /* build all the huffman tables */
70
    ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
71
                                 m->huff_code_dc_luminance,
72
                                 ff_mjpeg_bits_dc_luminance,
73
                                 ff_mjpeg_val_dc_luminance);
74
    ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
75
                                 m->huff_code_dc_chrominance,
76
                                 ff_mjpeg_bits_dc_chrominance,
77
                                 ff_mjpeg_val_dc_chrominance);
78
    ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
79
                                 m->huff_code_ac_luminance,
80
                                 ff_mjpeg_bits_ac_luminance,
81
                                 ff_mjpeg_val_ac_luminance);
82
    ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
83
                                 m->huff_code_ac_chrominance,
84
                                 ff_mjpeg_bits_ac_chrominance,
85
                                 ff_mjpeg_val_ac_chrominance);
86

    
87
    s->mjpeg_ctx = m;
88
    return 0;
89
}
90

    
91
void mjpeg_close(MpegEncContext *s)
92
{
93
    av_free(s->mjpeg_ctx);
94
}
95

    
96
/* table_class: 0 = DC coef, 1 = AC coefs */
97
static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
98
                             const uint8_t *bits_table, const uint8_t *value_table)
99
{
100
    PutBitContext *p = &s->pb;
101
    int n, i;
102

    
103
    put_bits(p, 4, table_class);
104
    put_bits(p, 4, table_id);
105

    
106
    n = 0;
107
    for(i=1;i<=16;i++) {
108
        n += bits_table[i];
109
        put_bits(p, 8, bits_table[i]);
110
    }
111

    
112
    for(i=0;i<n;i++)
113
        put_bits(p, 8, value_table[i]);
114

    
115
    return n + 17;
116
}
117

    
118
static void jpeg_table_header(MpegEncContext *s)
119
{
120
    PutBitContext *p = &s->pb;
121
    int i, j, size;
122
    uint8_t *ptr;
123

    
124
    /* quant matrixes */
125
    put_marker(p, DQT);
126
#ifdef TWOMATRIXES
127
    put_bits(p, 16, 2 + 2 * (1 + 64));
128
#else
129
    put_bits(p, 16, 2 + 1 * (1 + 64));
130
#endif
131
    put_bits(p, 4, 0); /* 8 bit precision */
132
    put_bits(p, 4, 0); /* table 0 */
133
    for(i=0;i<64;i++) {
134
        j = s->intra_scantable.permutated[i];
135
        put_bits(p, 8, s->intra_matrix[j]);
136
    }
137
#ifdef TWOMATRIXES
138
    put_bits(p, 4, 0); /* 8 bit precision */
139
    put_bits(p, 4, 1); /* table 1 */
140
    for(i=0;i<64;i++) {
141
        j = s->intra_scantable.permutated[i];
142
        put_bits(p, 8, s->chroma_intra_matrix[j]);
143
    }
144
#endif
145

    
146
    /* huffman table */
147
    put_marker(p, DHT);
148
    flush_put_bits(p);
149
    ptr = pbBufPtr(p);
150
    put_bits(p, 16, 0); /* patched later */
151
    size = 2;
152
    size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
153
                              ff_mjpeg_val_dc_luminance);
154
    size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
155
                              ff_mjpeg_val_dc_chrominance);
156

    
157
    size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
158
                              ff_mjpeg_val_ac_luminance);
159
    size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
160
                              ff_mjpeg_val_ac_chrominance);
161
    ptr[0] = size >> 8;
162
    ptr[1] = size;
163
}
164

    
165
static void jpeg_put_comments(MpegEncContext *s)
166
{
167
    PutBitContext *p = &s->pb;
168
    int size;
169
    uint8_t *ptr;
170

    
171
    if (s->aspect_ratio_info /* && !lossless */)
172
    {
173
    /* JFIF header */
174
    put_marker(p, APP0);
175
    put_bits(p, 16, 16);
176
    ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
177
    put_bits(p, 16, 0x0201); /* v 1.02 */
178
    put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
179
    put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
180
    put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
181
    put_bits(p, 8, 0); /* thumbnail width */
182
    put_bits(p, 8, 0); /* thumbnail height */
183
    }
184

    
185
    /* comment */
186
    if(!(s->flags & CODEC_FLAG_BITEXACT)){
187
        put_marker(p, COM);
188
        flush_put_bits(p);
189
        ptr = pbBufPtr(p);
190
        put_bits(p, 16, 0); /* patched later */
191
        ff_put_string(p, LIBAVCODEC_IDENT, 1);
192
        size = strlen(LIBAVCODEC_IDENT)+3;
193
        ptr[0] = size >> 8;
194
        ptr[1] = size;
195
    }
196

    
197
    if(  s->avctx->pix_fmt == PIX_FMT_YUV420P
198
       ||s->avctx->pix_fmt == PIX_FMT_YUV422P
199
       ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
200
        put_marker(p, COM);
201
        flush_put_bits(p);
202
        ptr = pbBufPtr(p);
203
        put_bits(p, 16, 0); /* patched later */
204
        ff_put_string(p, "CS=ITU601", 1);
205
        size = strlen("CS=ITU601")+3;
206
        ptr[0] = size >> 8;
207
        ptr[1] = size;
208
    }
209
}
210

    
211
void mjpeg_picture_header(MpegEncContext *s)
212
{
213
    const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
214

    
215
    put_marker(&s->pb, SOI);
216

    
217
    jpeg_put_comments(s);
218

    
219
    jpeg_table_header(s);
220

    
221
    switch(s->avctx->codec_id){
222
    case CODEC_ID_MJPEG:  put_marker(&s->pb, SOF0 ); break;
223
    case CODEC_ID_LJPEG:  put_marker(&s->pb, SOF3 ); break;
224
    default: assert(0);
225
    }
226

    
227
    put_bits(&s->pb, 16, 17);
228
    if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
229
        put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
230
    else
231
        put_bits(&s->pb, 8, 8); /* 8 bits/component */
232
    put_bits(&s->pb, 16, s->height);
233
    put_bits(&s->pb, 16, s->width);
234
    put_bits(&s->pb, 8, 3); /* 3 components */
235

    
236
    /* Y component */
237
    put_bits(&s->pb, 8, 1); /* component number */
238
    put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
239
    put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
240
    put_bits(&s->pb, 8, 0); /* select matrix */
241

    
242
    /* Cb component */
243
    put_bits(&s->pb, 8, 2); /* component number */
244
    put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
245
    put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
246
#ifdef TWOMATRIXES
247
    put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
248
#else
249
    put_bits(&s->pb, 8, 0); /* select matrix */
250
#endif
251

    
252
    /* Cr component */
253
    put_bits(&s->pb, 8, 3); /* component number */
254
    put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
255
    put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
256
#ifdef TWOMATRIXES
257
    put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
258
#else
259
    put_bits(&s->pb, 8, 0); /* select matrix */
260
#endif
261

    
262
    /* scan header */
263
    put_marker(&s->pb, SOS);
264
    put_bits(&s->pb, 16, 12); /* length */
265
    put_bits(&s->pb, 8, 3); /* 3 components */
266

    
267
    /* Y component */
268
    put_bits(&s->pb, 8, 1); /* index */
269
    put_bits(&s->pb, 4, 0); /* DC huffman table index */
270
    put_bits(&s->pb, 4, 0); /* AC huffman table index */
271

    
272
    /* Cb component */
273
    put_bits(&s->pb, 8, 2); /* index */
274
    put_bits(&s->pb, 4, 1); /* DC huffman table index */
275
    put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
276

    
277
    /* Cr component */
278
    put_bits(&s->pb, 8, 3); /* index */
279
    put_bits(&s->pb, 4, 1); /* DC huffman table index */
280
    put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
281

    
282
    put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
283

    
284
    switch(s->avctx->codec_id){
285
    case CODEC_ID_MJPEG:  put_bits(&s->pb, 8, 63); break; /* Se (not used) */
286
    case CODEC_ID_LJPEG:  put_bits(&s->pb, 8,  0); break; /* not used */
287
    default: assert(0);
288
    }
289

    
290
    put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
291
}
292

    
293
static void escape_FF(MpegEncContext *s, int start)
294
{
295
    int size= put_bits_count(&s->pb) - start*8;
296
    int i, ff_count;
297
    uint8_t *buf= s->pb.buf + start;
298
    int align= (-(size_t)(buf))&3;
299

    
300
    assert((size&7) == 0);
301
    size >>= 3;
302

    
303
    ff_count=0;
304
    for(i=0; i<size && i<align; i++){
305
        if(buf[i]==0xFF) ff_count++;
306
    }
307
    for(; i<size-15; i+=16){
308
        int acc, v;
309

    
310
        v= *(uint32_t*)(&buf[i]);
311
        acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
312
        v= *(uint32_t*)(&buf[i+4]);
313
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
314
        v= *(uint32_t*)(&buf[i+8]);
315
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
316
        v= *(uint32_t*)(&buf[i+12]);
317
        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
318

    
319
        acc>>=4;
320
        acc+= (acc>>16);
321
        acc+= (acc>>8);
322
        ff_count+= acc&0xFF;
323
    }
324
    for(; i<size; i++){
325
        if(buf[i]==0xFF) ff_count++;
326
    }
327

    
328
    if(ff_count==0) return;
329

    
330
    /* skip put bits */
331
    for(i=0; i<ff_count-3; i+=4)
332
        put_bits(&s->pb, 32, 0);
333
    put_bits(&s->pb, (ff_count-i)*8, 0);
334
    flush_put_bits(&s->pb);
335

    
336
    for(i=size-1; ff_count; i--){
337
        int v= buf[i];
338

    
339
        if(v==0xFF){
340
//printf("%d %d\n", i, ff_count);
341
            buf[i+ff_count]= 0;
342
            ff_count--;
343
        }
344

    
345
        buf[i+ff_count]= v;
346
    }
347
}
348

    
349
void ff_mjpeg_stuffing(PutBitContext * pbc)
350
{
351
    int length;
352
    length= (-put_bits_count(pbc))&7;
353
    if(length) put_bits(pbc, length, (1<<length)-1);
354
}
355

    
356
void mjpeg_picture_trailer(MpegEncContext *s)
357
{
358
    ff_mjpeg_stuffing(&s->pb);
359
    flush_put_bits(&s->pb);
360

    
361
    assert((s->header_bits&7)==0);
362

    
363
    escape_FF(s, s->header_bits>>3);
364

    
365
    put_marker(&s->pb, EOI);
366
}
367

    
368
static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
369
                                   uint8_t *huff_size, uint16_t *huff_code)
370
{
371
    int mant, nbits;
372

    
373
    if (val == 0) {
374
        put_bits(&s->pb, huff_size[0], huff_code[0]);
375
    } else {
376
        mant = val;
377
        if (val < 0) {
378
            val = -val;
379
            mant--;
380
        }
381

    
382
        nbits= av_log2_16bit(val) + 1;
383

    
384
        put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
385

    
386
        put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
387
    }
388
}
389

    
390
static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
391
{
392
    int mant, nbits, code, i, j;
393
    int component, dc, run, last_index, val;
394
    MJpegContext *m = s->mjpeg_ctx;
395
    uint8_t *huff_size_ac;
396
    uint16_t *huff_code_ac;
397

    
398
    /* DC coef */
399
    component = (n <= 3 ? 0 : (n&1) + 1);
400
    dc = block[0]; /* overflow is impossible */
401
    val = dc - s->last_dc[component];
402
    if (n < 4) {
403
        mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
404
        huff_size_ac = m->huff_size_ac_luminance;
405
        huff_code_ac = m->huff_code_ac_luminance;
406
    } else {
407
        mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
408
        huff_size_ac = m->huff_size_ac_chrominance;
409
        huff_code_ac = m->huff_code_ac_chrominance;
410
    }
411
    s->last_dc[component] = dc;
412

    
413
    /* AC coefs */
414

    
415
    run = 0;
416
    last_index = s->block_last_index[n];
417
    for(i=1;i<=last_index;i++) {
418
        j = s->intra_scantable.permutated[i];
419
        val = block[j];
420
        if (val == 0) {
421
            run++;
422
        } else {
423
            while (run >= 16) {
424
                put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
425
                run -= 16;
426
            }
427
            mant = val;
428
            if (val < 0) {
429
                val = -val;
430
                mant--;
431
            }
432

    
433
            nbits= av_log2(val) + 1;
434
            code = (run << 4) | nbits;
435

    
436
            put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
437

    
438
            put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
439
            run = 0;
440
        }
441
    }
442

    
443
    /* output EOB only if not already 64 values */
444
    if (last_index < 63 || run != 0)
445
        put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
446
}
447

    
448
void mjpeg_encode_mb(MpegEncContext *s,
449
                     DCTELEM block[6][64])
450
{
451
    int i;
452
    for(i=0;i<5;i++) {
453
        encode_block(s, block[i], i);
454
    }
455
    if (s->chroma_format == CHROMA_420) {
456
        encode_block(s, block[5], 5);
457
    } else {
458
        encode_block(s, block[6], 6);
459
        encode_block(s, block[5], 5);
460
        encode_block(s, block[7], 7);
461
    }
462
}
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
};