ffmpeg / libavcodec / mjpegenc.c @ 4922a5b0
History  View  Annotate  Download (18.8 KB)
1 
/*


2 
* MJPEG encoder

3 
* Copyright (c) 2000, 2001 Fabrice Bellard.

4 
* Copyright (c) 2003 Alex Beregszaszi

5 
* Copyright (c) 20032004 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 021101301 USA

22 
*

23 
* Support for external huffman table, various fixes (AVID workaround),

24 
* aspecting, new decode_frame mechanism and apple mjpegb 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 
build_huffman_codes(m>huff_size_dc_luminance, 
71 
m>huff_code_dc_luminance, 
72 
bits_dc_luminance, 
73 
val_dc_luminance); 
74 
build_huffman_codes(m>huff_size_dc_chrominance, 
75 
m>huff_code_dc_chrominance, 
76 
bits_dc_chrominance, 
77 
val_dc_chrominance); 
78 
build_huffman_codes(m>huff_size_ac_luminance, 
79 
m>huff_code_ac_luminance, 
80 
bits_ac_luminance, 
81 
val_ac_luminance); 
82 
build_huffman_codes(m>huff_size_ac_chrominance, 
83 
m>huff_code_ac_chrominance, 
84 
bits_ac_chrominance, 
85 
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, bits_dc_luminance, val_dc_luminance); 
153 
size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance); 
154  
155 
size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance); 
156 
size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance); 
157 
ptr[0] = size >> 8; 
158 
ptr[1] = size;

159 
} 
160  
161 
static void jpeg_put_comments(MpegEncContext *s) 
162 
{ 
163 
PutBitContext *p = &s>pb; 
164 
int size;

165 
uint8_t *ptr; 
166  
167 
if (s>aspect_ratio_info /* && !lossless */) 
168 
{ 
169 
/* JFIF header */

170 
put_marker(p, APP0); 
171 
put_bits(p, 16, 16); 
172 
ff_put_string(p, "JFIF", 1); /* this puts the trailing zerobyte too */ 
173 
put_bits(p, 16, 0x0201); /* v 1.02 */ 
174 
put_bits(p, 8, 0); /* units type: 0  aspect ratio */ 
175 
put_bits(p, 16, s>avctx>sample_aspect_ratio.num);

176 
put_bits(p, 16, s>avctx>sample_aspect_ratio.den);

177 
put_bits(p, 8, 0); /* thumbnail width */ 
178 
put_bits(p, 8, 0); /* thumbnail height */ 
179 
} 
180  
181 
/* comment */

182 
if(!(s>flags & CODEC_FLAG_BITEXACT)){

183 
put_marker(p, COM); 
184 
flush_put_bits(p); 
185 
ptr = pbBufPtr(p); 
186 
put_bits(p, 16, 0); /* patched later */ 
187 
ff_put_string(p, LIBAVCODEC_IDENT, 1);

188 
size = strlen(LIBAVCODEC_IDENT)+3;

189 
ptr[0] = size >> 8; 
190 
ptr[1] = size;

191 
} 
192  
193 
if( s>avctx>pix_fmt == PIX_FMT_YUV420P

194 
s>avctx>pix_fmt == PIX_FMT_YUV422P 
195 
s>avctx>pix_fmt == PIX_FMT_YUV444P){ 
196 
put_marker(p, COM); 
197 
flush_put_bits(p); 
198 
ptr = pbBufPtr(p); 
199 
put_bits(p, 16, 0); /* patched later */ 
200 
ff_put_string(p, "CS=ITU601", 1); 
201 
size = strlen("CS=ITU601")+3; 
202 
ptr[0] = size >> 8; 
203 
ptr[1] = size;

204 
} 
205 
} 
206  
207 
void mjpeg_picture_header(MpegEncContext *s)

208 
{ 
209 
const int lossless= s>avctx>codec_id != CODEC_ID_MJPEG; 
210  
211 
put_marker(&s>pb, SOI); 
212  
213 
jpeg_put_comments(s); 
214  
215 
jpeg_table_header(s); 
216  
217 
switch(s>avctx>codec_id){

218 
case CODEC_ID_MJPEG: put_marker(&s>pb, SOF0 ); break; 
219 
case CODEC_ID_LJPEG: put_marker(&s>pb, SOF3 ); break; 
220 
default: assert(0); 
221 
} 
222  
223 
put_bits(&s>pb, 16, 17); 
224 
if(lossless && s>avctx>pix_fmt == PIX_FMT_RGB32)

225 
put_bits(&s>pb, 8, 9); /* 9 bits/component RCT */ 
226 
else

227 
put_bits(&s>pb, 8, 8); /* 8 bits/component */ 
228 
put_bits(&s>pb, 16, s>height);

229 
put_bits(&s>pb, 16, s>width);

230 
put_bits(&s>pb, 8, 3); /* 3 components */ 
231  
232 
/* Y component */

233 
put_bits(&s>pb, 8, 1); /* component number */ 
234 
put_bits(&s>pb, 4, s>mjpeg_hsample[0]); /* H factor */ 
235 
put_bits(&s>pb, 4, s>mjpeg_vsample[0]); /* V factor */ 
236 
put_bits(&s>pb, 8, 0); /* select matrix */ 
237  
238 
/* Cb component */

239 
put_bits(&s>pb, 8, 2); /* component number */ 
240 
put_bits(&s>pb, 4, s>mjpeg_hsample[1]); /* H factor */ 
241 
put_bits(&s>pb, 4, s>mjpeg_vsample[1]); /* V factor */ 
242 
#ifdef TWOMATRIXES

243 
put_bits(&s>pb, 8, lossless ? 0 : 1); /* select matrix */ 
244 
#else

245 
put_bits(&s>pb, 8, 0); /* select matrix */ 
246 
#endif

247  
248 
/* Cr component */

249 
put_bits(&s>pb, 8, 3); /* component number */ 
250 
put_bits(&s>pb, 4, s>mjpeg_hsample[2]); /* H factor */ 
251 
put_bits(&s>pb, 4, s>mjpeg_vsample[2]); /* V factor */ 
252 
#ifdef TWOMATRIXES

253 
put_bits(&s>pb, 8, lossless ? 0 : 1); /* select matrix */ 
254 
#else

255 
put_bits(&s>pb, 8, 0); /* select matrix */ 
256 
#endif

257  
258 
/* scan header */

259 
put_marker(&s>pb, SOS); 
260 
put_bits(&s>pb, 16, 12); /* length */ 
261 
put_bits(&s>pb, 8, 3); /* 3 components */ 
262  
263 
/* Y component */

264 
put_bits(&s>pb, 8, 1); /* index */ 
265 
put_bits(&s>pb, 4, 0); /* DC huffman table index */ 
266 
put_bits(&s>pb, 4, 0); /* AC huffman table index */ 
267  
268 
/* Cb component */

269 
put_bits(&s>pb, 8, 2); /* index */ 
270 
put_bits(&s>pb, 4, 1); /* DC huffman table index */ 
271 
put_bits(&s>pb, 4, lossless ? 0 : 1); /* AC huffman table index */ 
272  
273 
/* Cr component */

274 
put_bits(&s>pb, 8, 3); /* index */ 
275 
put_bits(&s>pb, 4, 1); /* DC huffman table index */ 
276 
put_bits(&s>pb, 4, lossless ? 0 : 1); /* AC huffman table index */ 
277  
278 
put_bits(&s>pb, 8, lossless ? s>avctx>prediction_method+1 : 0); /* Ss (not used) */ 
279  
280 
switch(s>avctx>codec_id){

281 
case CODEC_ID_MJPEG: put_bits(&s>pb, 8, 63); break; /* Se (not used) */ 
282 
case CODEC_ID_LJPEG: put_bits(&s>pb, 8, 0); break; /* not used */ 
283 
default: assert(0); 
284 
} 
285  
286 
put_bits(&s>pb, 8, 0); /* Ah/Al (not used) */ 
287 
} 
288  
289 
static void escape_FF(MpegEncContext *s, int start) 
290 
{ 
291 
int size= put_bits_count(&s>pb)  start*8; 
292 
int i, ff_count;

293 
uint8_t *buf= s>pb.buf + start; 
294 
int align= ((size_t)(buf))&3; 
295  
296 
assert((size&7) == 0); 
297 
size >>= 3;

298  
299 
ff_count=0;

300 
for(i=0; i<size && i<align; i++){ 
301 
if(buf[i]==0xFF) ff_count++; 
302 
} 
303 
for(; i<size15; i+=16){ 
304 
int acc, v;

305  
306 
v= *(uint32_t*)(&buf[i]); 
307 
acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; 
308 
v= *(uint32_t*)(&buf[i+4]);

309 
acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; 
310 
v= *(uint32_t*)(&buf[i+8]);

311 
acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; 
312 
v= *(uint32_t*)(&buf[i+12]);

313 
acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; 
314  
315 
acc>>=4;

316 
acc+= (acc>>16);

317 
acc+= (acc>>8);

318 
ff_count+= acc&0xFF;

319 
} 
320 
for(; i<size; i++){

321 
if(buf[i]==0xFF) ff_count++; 
322 
} 
323  
324 
if(ff_count==0) return; 
325  
326 
/* skip put bits */

327 
for(i=0; i<ff_count3; i+=4) 
328 
put_bits(&s>pb, 32, 0); 
329 
put_bits(&s>pb, (ff_counti)*8, 0); 
330 
flush_put_bits(&s>pb); 
331  
332 
for(i=size1; ff_count; i){ 
333 
int v= buf[i];

334  
335 
if(v==0xFF){ 
336 
//printf("%d %d\n", i, ff_count);

337 
buf[i+ff_count]= 0;

338 
ff_count; 
339 
} 
340  
341 
buf[i+ff_count]= v; 
342 
} 
343 
} 
344  
345 
void ff_mjpeg_stuffing(PutBitContext * pbc)

346 
{ 
347 
int length;

348 
length= (put_bits_count(pbc))&7;

349 
if(length) put_bits(pbc, length, (1<<length)1); 
350 
} 
351  
352 
void mjpeg_picture_trailer(MpegEncContext *s)

353 
{ 
354 
ff_mjpeg_stuffing(&s>pb); 
355 
flush_put_bits(&s>pb); 
356  
357 
assert((s>header_bits&7)==0); 
358  
359 
escape_FF(s, s>header_bits>>3);

360  
361 
put_marker(&s>pb, EOI); 
362 
} 
363  
364 
static inline void mjpeg_encode_dc(MpegEncContext *s, int val, 
365 
uint8_t *huff_size, uint16_t *huff_code) 
366 
{ 
367 
int mant, nbits;

368  
369 
if (val == 0) { 
370 
put_bits(&s>pb, huff_size[0], huff_code[0]); 
371 
} else {

372 
mant = val; 
373 
if (val < 0) { 
374 
val = val; 
375 
mant; 
376 
} 
377  
378 
nbits= av_log2_16bit(val) + 1;

379  
380 
put_bits(&s>pb, huff_size[nbits], huff_code[nbits]); 
381  
382 
put_bits(&s>pb, nbits, mant & ((1 << nbits)  1)); 
383 
} 
384 
} 
385  
386 
static void encode_block(MpegEncContext *s, DCTELEM *block, int n) 
387 
{ 
388 
int mant, nbits, code, i, j;

389 
int component, dc, run, last_index, val;

390 
MJpegContext *m = s>mjpeg_ctx; 
391 
uint8_t *huff_size_ac; 
392 
uint16_t *huff_code_ac; 
393  
394 
/* DC coef */

395 
component = (n <= 3 ? 0 : (n&1) + 1); 
396 
dc = block[0]; /* overflow is impossible */ 
397 
val = dc  s>last_dc[component]; 
398 
if (n < 4) { 
399 
mjpeg_encode_dc(s, val, m>huff_size_dc_luminance, m>huff_code_dc_luminance); 
400 
huff_size_ac = m>huff_size_ac_luminance; 
401 
huff_code_ac = m>huff_code_ac_luminance; 
402 
} else {

403 
mjpeg_encode_dc(s, val, m>huff_size_dc_chrominance, m>huff_code_dc_chrominance); 
404 
huff_size_ac = m>huff_size_ac_chrominance; 
405 
huff_code_ac = m>huff_code_ac_chrominance; 
406 
} 
407 
s>last_dc[component] = dc; 
408  
409 
/* AC coefs */

410  
411 
run = 0;

412 
last_index = s>block_last_index[n]; 
413 
for(i=1;i<=last_index;i++) { 
414 
j = s>intra_scantable.permutated[i]; 
415 
val = block[j]; 
416 
if (val == 0) { 
417 
run++; 
418 
} else {

419 
while (run >= 16) { 
420 
put_bits(&s>pb, huff_size_ac[0xf0], huff_code_ac[0xf0]); 
421 
run = 16;

422 
} 
423 
mant = val; 
424 
if (val < 0) { 
425 
val = val; 
426 
mant; 
427 
} 
428  
429 
nbits= av_log2(val) + 1;

430 
code = (run << 4)  nbits;

431  
432 
put_bits(&s>pb, huff_size_ac[code], huff_code_ac[code]); 
433  
434 
put_bits(&s>pb, nbits, mant & ((1 << nbits)  1)); 
435 
run = 0;

436 
} 
437 
} 
438  
439 
/* output EOB only if not already 64 values */

440 
if (last_index < 63  run != 0) 
441 
put_bits(&s>pb, huff_size_ac[0], huff_code_ac[0]); 
442 
} 
443  
444 
void mjpeg_encode_mb(MpegEncContext *s,

445 
DCTELEM block[6][64]) 
446 
{ 
447 
int i;

448 
for(i=0;i<5;i++) { 
449 
encode_block(s, block[i], i); 
450 
} 
451 
if (s>chroma_format == CHROMA_420) {

452 
encode_block(s, block[5], 5); 
453 
} else {

454 
encode_block(s, block[6], 6); 
455 
encode_block(s, block[5], 5); 
456 
encode_block(s, block[7], 7); 
457 
} 
458 
} 
459  
460 
static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ 
461 
MpegEncContext * const s = avctx>priv_data;

462 
MJpegContext * const m = s>mjpeg_ctx;

463 
AVFrame *pict = data; 
464 
const int width= s>width; 
465 
const int height= s>height; 
466 
AVFrame * const p= (AVFrame*)&s>current_picture;

467 
const int predictor= avctx>prediction_method+1; 
468  
469 
init_put_bits(&s>pb, buf, buf_size); 
470  
471 
*p = *pict; 
472 
p>pict_type= FF_I_TYPE; 
473 
p>key_frame= 1;

474  
475 
mjpeg_picture_header(s); 
476  
477 
s>header_bits= put_bits_count(&s>pb); 
478  
479 
if(avctx>pix_fmt == PIX_FMT_RGB32){

480 
int x, y, i;

481 
const int linesize= p>linesize[0]; 
482 
uint16_t (*buffer)[4]= (void *) s>rd_scratchpad; 
483 
int left[3], top[3], topleft[3]; 
484  
485 
for(i=0; i<3; i++){ 
486 
buffer[0][i]= 1 << (9  1); 
487 
} 
488  
489 
for(y = 0; y < height; y++) { 
490 
const int modified_predictor= y ? predictor : 1; 
491 
uint8_t *ptr = p>data[0] + (linesize * y);

492  
493 
if(s>pb.buf_end  s>pb.buf  (put_bits_count(&s>pb)>>3) < width*3*4){ 
494 
av_log(s>avctx, AV_LOG_ERROR, "encoded frame too large\n");

495 
return 1; 
496 
} 
497  
498 
for(i=0; i<3; i++){ 
499 
top[i]= left[i]= topleft[i]= buffer[0][i];

500 
} 
501 
for(x = 0; x < width; x++) { 
502 
buffer[x][1] = ptr[4*x+0]  ptr[4*x+1] + 0x100; 
503 
buffer[x][2] = ptr[4*x+2]  ptr[4*x+1] + 0x100; 
504 
buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2; 
505  
506 
for(i=0;i<3;i++) { 
507 
int pred, diff;

508  
509 
PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); 
510  
511 
topleft[i]= top[i]; 
512 
top[i]= buffer[x+1][i];

513  
514 
left[i]= buffer[x][i]; 
515  
516 
diff= ((left[i]  pred + 0x100)&0x1FF)  0x100; 
517  
518 
if(i==0) 
519 
mjpeg_encode_dc(s, diff, m>huff_size_dc_luminance, m>huff_code_dc_luminance); //FIXME ugly

520 
else

521 
mjpeg_encode_dc(s, diff, m>huff_size_dc_chrominance, m>huff_code_dc_chrominance); 
522 
} 
523 
} 
524 
} 
525 
}else{

526 
int mb_x, mb_y, i;

527 
const int mb_width = (width + s>mjpeg_hsample[0]  1) / s>mjpeg_hsample[0]; 
528 
const int mb_height = (height + s>mjpeg_vsample[0]  1) / s>mjpeg_vsample[0]; 
529  
530 
for(mb_y = 0; mb_y < mb_height; mb_y++) { 
531 
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]){ 
532 
av_log(s>avctx, AV_LOG_ERROR, "encoded frame too large\n");

533 
return 1; 
534 
} 
535 
for(mb_x = 0; mb_x < mb_width; mb_x++) { 
536 
if(mb_x==0  mb_y==0){ 
537 
for(i=0;i<3;i++) { 
538 
uint8_t *ptr; 
539 
int x, y, h, v, linesize;

540 
h = s>mjpeg_hsample[i]; 
541 
v = s>mjpeg_vsample[i]; 
542 
linesize= p>linesize[i]; 
543  
544 
for(y=0; y<v; y++){ 
545 
for(x=0; x<h; x++){ 
546 
int pred;

547  
548 
ptr = p>data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap

549 
if(y==0 && mb_y==0){ 
550 
if(x==0 && mb_x==0){ 
551 
pred= 128;

552 
}else{

553 
pred= ptr[1];

554 
} 
555 
}else{

556 
if(x==0 && mb_x==0){ 
557 
pred= ptr[linesize]; 
558 
}else{

559 
PREDICT(pred, ptr[linesize1], ptr[linesize], ptr[1], predictor); 
560 
} 
561 
} 
562  
563 
if(i==0) 
564 
mjpeg_encode_dc(s, (int8_t)(*ptr  pred), m>huff_size_dc_luminance, m>huff_code_dc_luminance); //FIXME ugly

565 
else

566 
mjpeg_encode_dc(s, (int8_t)(*ptr  pred), m>huff_size_dc_chrominance, m>huff_code_dc_chrominance); 
567 
} 
568 
} 
569 
} 
570 
}else{

571 
for(i=0;i<3;i++) { 
572 
uint8_t *ptr; 
573 
int x, y, h, v, linesize;

574 
h = s>mjpeg_hsample[i]; 
575 
v = s>mjpeg_vsample[i]; 
576 
linesize= p>linesize[i]; 
577  
578 
for(y=0; y<v; y++){ 
579 
for(x=0; x<h; x++){ 
580 
int pred;

581  
582 
ptr = p>data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap

583 
//printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);

584 
PREDICT(pred, ptr[linesize1], ptr[linesize], ptr[1], predictor); 
585  
586 
if(i==0) 
587 
mjpeg_encode_dc(s, (int8_t)(*ptr  pred), m>huff_size_dc_luminance, m>huff_code_dc_luminance); //FIXME ugly

588 
else

589 
mjpeg_encode_dc(s, (int8_t)(*ptr  pred), m>huff_size_dc_chrominance, m>huff_code_dc_chrominance); 
590 
} 
591 
} 
592 
} 
593 
} 
594 
} 
595 
} 
596 
} 
597  
598 
emms_c(); 
599  
600 
mjpeg_picture_trailer(s); 
601 
s>picture_number++; 
602  
603 
flush_put_bits(&s>pb); 
604 
return pbBufPtr(&s>pb)  s>pb.buf;

605 
// return (put_bits_count(&f>pb)+7)/8;

606 
} 
607  
608  
609 
AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them

610 
"ljpeg",

611 
CODEC_TYPE_VIDEO, 
612 
CODEC_ID_LJPEG, 
613 
sizeof(MpegEncContext),

614 
MPV_encode_init, 
615 
encode_picture_lossless, 
616 
MPV_encode_end, 
617 
}; 