00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031
00032 #include <assert.h>
00033
00034 #include "avcodec.h"
00035 #include "dsputil.h"
00036 #include "mpegvideo.h"
00037
00038
00039
00040 #undef TWOMATRIXES
00041
00042 typedef struct MJpegContext {
00043 uint8_t huff_size_dc_luminance[12];
00044 uint16_t huff_code_dc_luminance[12];
00045 uint8_t huff_size_dc_chrominance[12];
00046 uint16_t huff_code_dc_chrominance[12];
00047
00048 uint8_t huff_size_ac_luminance[256];
00049 uint16_t huff_code_ac_luminance[256];
00050 uint8_t huff_size_ac_chrominance[256];
00051 uint16_t huff_code_ac_chrominance[256];
00052 } MJpegContext;
00053
00054
00055 typedef enum {
00056
00057 SOF0 = 0xc0,
00058 SOF1 = 0xc1,
00059 SOF2 = 0xc2,
00060 SOF3 = 0xc3,
00061
00062 SOF5 = 0xc5,
00063 SOF6 = 0xc6,
00064 SOF7 = 0xc7,
00065 JPG = 0xc8,
00066 SOF9 = 0xc9,
00067 SOF10 = 0xca,
00068 SOF11 = 0xcb,
00069
00070 SOF13 = 0xcd,
00071 SOF14 = 0xce,
00072 SOF15 = 0xcf,
00073
00074 DHT = 0xc4,
00075
00076 DAC = 0xcc,
00077
00078
00079 RST0 = 0xd0,
00080 RST1 = 0xd1,
00081 RST2 = 0xd2,
00082 RST3 = 0xd3,
00083 RST4 = 0xd4,
00084 RST5 = 0xd5,
00085 RST6 = 0xd6,
00086 RST7 = 0xd7,
00087
00088 SOI = 0xd8,
00089 EOI = 0xd9,
00090 SOS = 0xda,
00091 DQT = 0xdb,
00092 DNL = 0xdc,
00093 DRI = 0xdd,
00094 DHP = 0xde,
00095 EXP = 0xdf,
00096
00097 APP0 = 0xe0,
00098 APP1 = 0xe1,
00099 APP2 = 0xe2,
00100 APP3 = 0xe3,
00101 APP4 = 0xe4,
00102 APP5 = 0xe5,
00103 APP6 = 0xe6,
00104 APP7 = 0xe7,
00105 APP8 = 0xe8,
00106 APP9 = 0xe9,
00107 APP10 = 0xea,
00108 APP11 = 0xeb,
00109 APP12 = 0xec,
00110 APP13 = 0xed,
00111 APP14 = 0xee,
00112 APP15 = 0xef,
00113
00114 JPG0 = 0xf0,
00115 JPG1 = 0xf1,
00116 JPG2 = 0xf2,
00117 JPG3 = 0xf3,
00118 JPG4 = 0xf4,
00119 JPG5 = 0xf5,
00120 JPG6 = 0xf6,
00121 JPG7 = 0xf7,
00122 JPG8 = 0xf8,
00123 JPG9 = 0xf9,
00124 JPG10 = 0xfa,
00125 JPG11 = 0xfb,
00126 JPG12 = 0xfc,
00127 JPG13 = 0xfd,
00128
00129 COM = 0xfe,
00130
00131 TEM = 0x01,
00132
00133
00134 } JPEG_MARKER;
00135
00136 #if 0
00137
00138
00139
00140
00141 static const unsigned char std_luminance_quant_tbl[64] = {
00142 16, 11, 10, 16, 24, 40, 51, 61,
00143 12, 12, 14, 19, 26, 58, 60, 55,
00144 14, 13, 16, 24, 40, 57, 69, 56,
00145 14, 17, 22, 29, 51, 87, 80, 62,
00146 18, 22, 37, 56, 68, 109, 103, 77,
00147 24, 35, 55, 64, 81, 104, 113, 92,
00148 49, 64, 78, 87, 103, 121, 120, 101,
00149 72, 92, 95, 98, 112, 100, 103, 99
00150 };
00151 static const unsigned char std_chrominance_quant_tbl[64] = {
00152 17, 18, 24, 47, 99, 99, 99, 99,
00153 18, 21, 26, 66, 99, 99, 99, 99,
00154 24, 26, 56, 99, 99, 99, 99, 99,
00155 47, 66, 99, 99, 99, 99, 99, 99,
00156 99, 99, 99, 99, 99, 99, 99, 99,
00157 99, 99, 99, 99, 99, 99, 99, 99,
00158 99, 99, 99, 99, 99, 99, 99, 99,
00159 99, 99, 99, 99, 99, 99, 99, 99
00160 };
00161 #endif
00162
00163
00164
00165 static const uint8_t bits_dc_luminance[17] =
00166 { 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
00167 static const uint8_t val_dc_luminance[] =
00168 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
00169
00170 static const uint8_t bits_dc_chrominance[17] =
00171 { 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
00172 static const uint8_t val_dc_chrominance[] =
00173 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
00174
00175 static const uint8_t bits_ac_luminance[17] =
00176 { 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
00177 static const uint8_t val_ac_luminance[] =
00178 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
00179 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
00180 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
00181 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
00182 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
00183 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
00184 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
00185 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
00186 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
00187 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00188 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00189 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
00190 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
00191 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
00192 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
00193 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
00194 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
00195 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
00196 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
00197 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00198 0xf9, 0xfa
00199 };
00200
00201 static const uint8_t bits_ac_chrominance[17] =
00202 { 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
00203
00204 static const uint8_t val_ac_chrominance[] =
00205 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
00206 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
00207 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
00208 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
00209 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
00210 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
00211 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
00212 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
00213 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
00214 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
00215 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
00216 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00217 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
00218 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
00219 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
00220 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
00221 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
00222 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
00223 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
00224 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00225 0xf9, 0xfa
00226 };
00227
00228
00229 static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
00230 const uint8_t *bits_table, const uint8_t *val_table)
00231 {
00232 int i, j, k,nb, code, sym;
00233
00234 code = 0;
00235 k = 0;
00236 for(i=1;i<=16;i++) {
00237 nb = bits_table[i];
00238 for(j=0;j<nb;j++) {
00239 sym = val_table[k++];
00240 huff_size[sym] = i;
00241 huff_code[sym] = code;
00242 code++;
00243 }
00244 code <<= 1;
00245 }
00246 }
00247
00248 #ifdef CONFIG_ENCODERS
00249 int mjpeg_init(MpegEncContext *s)
00250 {
00251 MJpegContext *m;
00252
00253 m = av_malloc(sizeof(MJpegContext));
00254 if (!m)
00255 return -1;
00256
00257 s->min_qcoeff=-1023;
00258 s->max_qcoeff= 1023;
00259
00260
00261 build_huffman_codes(m->huff_size_dc_luminance,
00262 m->huff_code_dc_luminance,
00263 bits_dc_luminance,
00264 val_dc_luminance);
00265 build_huffman_codes(m->huff_size_dc_chrominance,
00266 m->huff_code_dc_chrominance,
00267 bits_dc_chrominance,
00268 val_dc_chrominance);
00269 build_huffman_codes(m->huff_size_ac_luminance,
00270 m->huff_code_ac_luminance,
00271 bits_ac_luminance,
00272 val_ac_luminance);
00273 build_huffman_codes(m->huff_size_ac_chrominance,
00274 m->huff_code_ac_chrominance,
00275 bits_ac_chrominance,
00276 val_ac_chrominance);
00277
00278 s->mjpeg_ctx = m;
00279 return 0;
00280 }
00281
00282 void mjpeg_close(MpegEncContext *s)
00283 {
00284 av_free(s->mjpeg_ctx);
00285 }
00286 #endif //CONFIG_ENCODERS
00287
00288 #define PREDICT(ret, topleft, top, left, predictor)\
00289 switch(predictor){\
00290 case 1: ret= left; break;\
00291 case 2: ret= top; break;\
00292 case 3: ret= topleft; break;\
00293 case 4: ret= left + top - topleft; break;\
00294 case 5: ret= left + ((top - topleft)>>1); break;\
00295 case 6: ret= top + ((left - topleft)>>1); break;\
00296 default:\
00297 case 7: ret= (left + top)>>1; break;\
00298 }
00299
00300 #ifdef CONFIG_ENCODERS
00301 static inline void put_marker(PutBitContext *p, int code)
00302 {
00303 put_bits(p, 8, 0xff);
00304 put_bits(p, 8, code);
00305 }
00306
00307
00308 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
00309 const uint8_t *bits_table, const uint8_t *value_table)
00310 {
00311 PutBitContext *p = &s->pb;
00312 int n, i;
00313
00314 put_bits(p, 4, table_class);
00315 put_bits(p, 4, table_id);
00316
00317 n = 0;
00318 for(i=1;i<=16;i++) {
00319 n += bits_table[i];
00320 put_bits(p, 8, bits_table[i]);
00321 }
00322
00323 for(i=0;i<n;i++)
00324 put_bits(p, 8, value_table[i]);
00325
00326 return n + 17;
00327 }
00328
00329 static void jpeg_table_header(MpegEncContext *s)
00330 {
00331 PutBitContext *p = &s->pb;
00332 int i, j, size;
00333 uint8_t *ptr;
00334
00335
00336 put_marker(p, DQT);
00337 #ifdef TWOMATRIXES
00338 put_bits(p, 16, 2 + 2 * (1 + 64));
00339 #else
00340 put_bits(p, 16, 2 + 1 * (1 + 64));
00341 #endif
00342 put_bits(p, 4, 0);
00343 put_bits(p, 4, 0);
00344 for(i=0;i<64;i++) {
00345 j = s->intra_scantable.permutated[i];
00346 put_bits(p, 8, s->intra_matrix[j]);
00347 }
00348 #ifdef TWOMATRIXES
00349 put_bits(p, 4, 0);
00350 put_bits(p, 4, 1);
00351 for(i=0;i<64;i++) {
00352 j = s->intra_scantable.permutated[i];
00353 put_bits(p, 8, s->chroma_intra_matrix[j]);
00354 }
00355 #endif
00356
00357
00358 put_marker(p, DHT);
00359 flush_put_bits(p);
00360 ptr = pbBufPtr(p);
00361 put_bits(p, 16, 0);
00362 size = 2;
00363 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
00364 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
00365
00366 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
00367 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
00368 ptr[0] = size >> 8;
00369 ptr[1] = size;
00370 }
00371
00372 static void jpeg_put_comments(MpegEncContext *s)
00373 {
00374 PutBitContext *p = &s->pb;
00375 int size;
00376 uint8_t *ptr;
00377
00378 if (s->aspect_ratio_info )
00379 {
00380
00381 put_marker(p, APP0);
00382 put_bits(p, 16, 16);
00383 put_string(p, "JFIF", 1);
00384 put_bits(p, 16, 0x0201);
00385 put_bits(p, 8, 0);
00386 put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
00387 put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
00388 put_bits(p, 8, 0);
00389 put_bits(p, 8, 0);
00390 }
00391
00392
00393 if(!(s->flags & CODEC_FLAG_BITEXACT)){
00394 put_marker(p, COM);
00395 flush_put_bits(p);
00396 ptr = pbBufPtr(p);
00397 put_bits(p, 16, 0);
00398 put_string(p, LIBAVCODEC_IDENT, 1);
00399 size = strlen(LIBAVCODEC_IDENT)+3;
00400 ptr[0] = size >> 8;
00401 ptr[1] = size;
00402 }
00403
00404 if( s->avctx->pix_fmt == PIX_FMT_YUV420P
00405 ||s->avctx->pix_fmt == PIX_FMT_YUV422P
00406 ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
00407 put_marker(p, COM);
00408 flush_put_bits(p);
00409 ptr = pbBufPtr(p);
00410 put_bits(p, 16, 0);
00411 put_string(p, "CS=ITU601", 1);
00412 size = strlen("CS=ITU601")+3;
00413 ptr[0] = size >> 8;
00414 ptr[1] = size;
00415 }
00416 }
00417
00418 void mjpeg_picture_header(MpegEncContext *s)
00419 {
00420 const int lossless= s->avctx->codec_id == CODEC_ID_LJPEG;
00421
00422 put_marker(&s->pb, SOI);
00423
00424 if (!s->mjpeg_data_only_frames)
00425 {
00426 jpeg_put_comments(s);
00427
00428 if (s->mjpeg_write_tables) jpeg_table_header(s);
00429
00430 put_marker(&s->pb, lossless ? SOF3 : SOF0);
00431
00432 put_bits(&s->pb, 16, 17);
00433 if(lossless && s->avctx->pix_fmt == PIX_FMT_RGBA32)
00434 put_bits(&s->pb, 8, 9);
00435 else
00436 put_bits(&s->pb, 8, 8);
00437 put_bits(&s->pb, 16, s->height);
00438 put_bits(&s->pb, 16, s->width);
00439 put_bits(&s->pb, 8, 3);
00440
00441
00442 put_bits(&s->pb, 8, 1);
00443 put_bits(&s->pb, 4, s->mjpeg_hsample[0]);
00444 put_bits(&s->pb, 4, s->mjpeg_vsample[0]);
00445 put_bits(&s->pb, 8, 0);
00446
00447
00448 put_bits(&s->pb, 8, 2);
00449 put_bits(&s->pb, 4, s->mjpeg_hsample[1]);
00450 put_bits(&s->pb, 4, s->mjpeg_vsample[1]);
00451 #ifdef TWOMATRIXES
00452 put_bits(&s->pb, 8, lossless ? 0 : 1);
00453 #else
00454 put_bits(&s->pb, 8, 0);
00455 #endif
00456
00457
00458 put_bits(&s->pb, 8, 3);
00459 put_bits(&s->pb, 4, s->mjpeg_hsample[2]);
00460 put_bits(&s->pb, 4, s->mjpeg_vsample[2]);
00461 #ifdef TWOMATRIXES
00462 put_bits(&s->pb, 8, lossless ? 0 : 1);
00463 #else
00464 put_bits(&s->pb, 8, 0);
00465 #endif
00466 }
00467
00468
00469 put_marker(&s->pb, SOS);
00470 put_bits(&s->pb, 16, 12);
00471 put_bits(&s->pb, 8, 3);
00472
00473
00474 put_bits(&s->pb, 8, 1);
00475 put_bits(&s->pb, 4, 0);
00476 put_bits(&s->pb, 4, 0);
00477
00478
00479 put_bits(&s->pb, 8, 2);
00480 put_bits(&s->pb, 4, 1);
00481 put_bits(&s->pb, 4, lossless ? 0 : 1);
00482
00483
00484 put_bits(&s->pb, 8, 3);
00485 put_bits(&s->pb, 4, 1);
00486 put_bits(&s->pb, 4, lossless ? 0 : 1);
00487
00488 put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0);
00489 put_bits(&s->pb, 8, lossless ? 0 : 63);
00490 put_bits(&s->pb, 8, 0);
00491 }
00492
00493 static void escape_FF(MpegEncContext *s, int start)
00494 {
00495 int size= put_bits_count(&s->pb) - start*8;
00496 int i, ff_count;
00497 uint8_t *buf= s->pb.buf + start;
00498 int align= (-(size_t)(buf))&3;
00499
00500 assert((size&7) == 0);
00501 size >>= 3;
00502
00503 ff_count=0;
00504 for(i=0; i<size && i<align; i++){
00505 if(buf[i]==0xFF) ff_count++;
00506 }
00507 for(; i<size-15; i+=16){
00508 int acc, v;
00509
00510 v= *(uint32_t*)(&buf[i]);
00511 acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00512 v= *(uint32_t*)(&buf[i+4]);
00513 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00514 v= *(uint32_t*)(&buf[i+8]);
00515 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00516 v= *(uint32_t*)(&buf[i+12]);
00517 acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00518
00519 acc>>=4;
00520 acc+= (acc>>16);
00521 acc+= (acc>>8);
00522 ff_count+= acc&0xFF;
00523 }
00524 for(; i<size; i++){
00525 if(buf[i]==0xFF) ff_count++;
00526 }
00527
00528 if(ff_count==0) return;
00529
00530
00531 for(i=0; i<ff_count-3; i+=4)
00532 put_bits(&s->pb, 32, 0);
00533 put_bits(&s->pb, (ff_count-i)*8, 0);
00534 flush_put_bits(&s->pb);
00535
00536 for(i=size-1; ff_count; i--){
00537 int v= buf[i];
00538
00539 if(v==0xFF){
00540
00541 buf[i+ff_count]= 0;
00542 ff_count--;
00543 }
00544
00545 buf[i+ff_count]= v;
00546 }
00547 }
00548
00549 void ff_mjpeg_stuffing(PutBitContext * pbc)
00550 {
00551 int length;
00552 length= (-put_bits_count(pbc))&7;
00553 if(length) put_bits(pbc, length, (1<<length)-1);
00554 }
00555
00556 void mjpeg_picture_trailer(MpegEncContext *s)
00557 {
00558 ff_mjpeg_stuffing(&s->pb);
00559 flush_put_bits(&s->pb);
00560
00561 assert((s->header_bits&7)==0);
00562
00563 escape_FF(s, s->header_bits>>3);
00564
00565 put_marker(&s->pb, EOI);
00566 }
00567
00568 static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
00569 uint8_t *huff_size, uint16_t *huff_code)
00570 {
00571 int mant, nbits;
00572
00573 if (val == 0) {
00574 put_bits(&s->pb, huff_size[0], huff_code[0]);
00575 } else {
00576 mant = val;
00577 if (val < 0) {
00578 val = -val;
00579 mant--;
00580 }
00581
00582 nbits= av_log2_16bit(val) + 1;
00583
00584 put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
00585
00586 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
00587 }
00588 }
00589
00590 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
00591 {
00592 int mant, nbits, code, i, j;
00593 int component, dc, run, last_index, val;
00594 MJpegContext *m = s->mjpeg_ctx;
00595 uint8_t *huff_size_ac;
00596 uint16_t *huff_code_ac;
00597
00598
00599 component = (n <= 3 ? 0 : n - 4 + 1);
00600 dc = block[0];
00601 val = dc - s->last_dc[component];
00602 if (n < 4) {
00603 mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00604 huff_size_ac = m->huff_size_ac_luminance;
00605 huff_code_ac = m->huff_code_ac_luminance;
00606 } else {
00607 mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00608 huff_size_ac = m->huff_size_ac_chrominance;
00609 huff_code_ac = m->huff_code_ac_chrominance;
00610 }
00611 s->last_dc[component] = dc;
00612
00613
00614
00615 run = 0;
00616 last_index = s->block_last_index[n];
00617 for(i=1;i<=last_index;i++) {
00618 j = s->intra_scantable.permutated[i];
00619 val = block[j];
00620 if (val == 0) {
00621 run++;
00622 } else {
00623 while (run >= 16) {
00624 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
00625 run -= 16;
00626 }
00627 mant = val;
00628 if (val < 0) {
00629 val = -val;
00630 mant--;
00631 }
00632
00633 nbits= av_log2(val) + 1;
00634 code = (run << 4) | nbits;
00635
00636 put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
00637
00638 put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
00639 run = 0;
00640 }
00641 }
00642
00643
00644 if (last_index < 63 || run != 0)
00645 put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
00646 }
00647
00648 void mjpeg_encode_mb(MpegEncContext *s,
00649 DCTELEM block[6][64])
00650 {
00651 int i;
00652 for(i=0;i<6;i++) {
00653 encode_block(s, block[i], i);
00654 }
00655 }
00656
00657 static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00658 MpegEncContext * const s = avctx->priv_data;
00659 MJpegContext * const m = s->mjpeg_ctx;
00660 AVFrame *pict = data;
00661 const int width= s->width;
00662 const int height= s->height;
00663 AVFrame * const p= (AVFrame*)&s->current_picture;
00664 const int predictor= avctx->prediction_method+1;
00665
00666 init_put_bits(&s->pb, buf, buf_size);
00667
00668 *p = *pict;
00669 p->pict_type= FF_I_TYPE;
00670 p->key_frame= 1;
00671
00672 mjpeg_picture_header(s);
00673
00674 s->header_bits= put_bits_count(&s->pb);
00675
00676 if(avctx->pix_fmt == PIX_FMT_RGBA32){
00677 int x, y, i;
00678 const int linesize= p->linesize[0];
00679 uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
00680 int left[3], top[3], topleft[3];
00681
00682 for(i=0; i<3; i++){
00683 buffer[0][i]= 1 << (9 - 1);
00684 }
00685
00686 for(y = 0; y < height; y++) {
00687 const int modified_predictor= y ? predictor : 1;
00688 uint8_t *ptr = p->data[0] + (linesize * y);
00689
00690 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
00691 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00692 return -1;
00693 }
00694
00695 for(i=0; i<3; i++){
00696 top[i]= left[i]= topleft[i]= buffer[0][i];
00697 }
00698 for(x = 0; x < width; x++) {
00699 buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
00700 buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
00701 buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
00702
00703 for(i=0;i<3;i++) {
00704 int pred, diff;
00705
00706 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
00707
00708 topleft[i]= top[i];
00709 top[i]= buffer[x+1][i];
00710
00711 left[i]= buffer[x][i];
00712
00713 diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
00714
00715 if(i==0)
00716 mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00717 else
00718 mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00719 }
00720 }
00721 }
00722 }else{
00723 int mb_x, mb_y, i;
00724 const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
00725 const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
00726
00727 for(mb_y = 0; mb_y < mb_height; mb_y++) {
00728 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]){
00729 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00730 return -1;
00731 }
00732 for(mb_x = 0; mb_x < mb_width; mb_x++) {
00733 if(mb_x==0 || mb_y==0){
00734 for(i=0;i<3;i++) {
00735 uint8_t *ptr;
00736 int x, y, h, v, linesize;
00737 h = s->mjpeg_hsample[i];
00738 v = s->mjpeg_vsample[i];
00739 linesize= p->linesize[i];
00740
00741 for(y=0; y<v; y++){
00742 for(x=0; x<h; x++){
00743 int pred;
00744
00745 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
00746 if(y==0 && mb_y==0){
00747 if(x==0 && mb_x==0){
00748 pred= 128;
00749 }else{
00750 pred= ptr[-1];
00751 }
00752 }else{
00753 if(x==0 && mb_x==0){
00754 pred= ptr[-linesize];
00755 }else{
00756 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
00757 }
00758 }
00759
00760 if(i==0)
00761 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00762 else
00763 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00764 }
00765 }
00766 }
00767 }else{
00768 for(i=0;i<3;i++) {
00769 uint8_t *ptr;
00770 int x, y, h, v, linesize;
00771 h = s->mjpeg_hsample[i];
00772 v = s->mjpeg_vsample[i];
00773 linesize= p->linesize[i];
00774
00775 for(y=0; y<v; y++){
00776 for(x=0; x<h; x++){
00777 int pred;
00778
00779 ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
00780
00781 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
00782
00783 if(i==0)
00784 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00785 else
00786 mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00787 }
00788 }
00789 }
00790 }
00791 }
00792 }
00793 }
00794
00795 emms_c();
00796
00797 mjpeg_picture_trailer(s);
00798 s->picture_number++;
00799
00800 flush_put_bits(&s->pb);
00801 return pbBufPtr(&s->pb) - s->pb.buf;
00802
00803 }
00804
00805 #endif //CONFIG_ENCODERS
00806
00807
00808
00809
00810 #define MAX_COMPONENTS 4
00811
00812 typedef struct MJpegDecodeContext {
00813 AVCodecContext *avctx;
00814 GetBitContext gb;
00815 int mpeg_enc_ctx_allocated;
00816
00817 int start_code;
00818 int buffer_size;
00819 uint8_t *buffer;
00820
00821 int16_t quant_matrixes[4][64];
00822 VLC vlcs[2][4];
00823 int qscale[4];
00824
00825 int org_height;
00826 int first_picture;
00827 int interlaced;
00828 int bottom_field;
00829 int lossless;
00830 int rgb;
00831 int rct;
00832 int pegasus_rct;
00833 int bits;
00834
00835 int width, height;
00836 int mb_width, mb_height;
00837 int nb_components;
00838 int component_id[MAX_COMPONENTS];
00839 int h_count[MAX_COMPONENTS];
00840 int v_count[MAX_COMPONENTS];
00841 int comp_index[MAX_COMPONENTS];
00842 int dc_index[MAX_COMPONENTS];
00843 int ac_index[MAX_COMPONENTS];
00844 int nb_blocks[MAX_COMPONENTS];
00845 int h_scount[MAX_COMPONENTS];
00846 int v_scount[MAX_COMPONENTS];
00847 int h_max, v_max;
00848 int quant_index[4];
00849 int last_dc[MAX_COMPONENTS];
00850 AVFrame picture;
00851 int linesize[MAX_COMPONENTS];
00852 int8_t *qscale_table;
00853 DCTELEM block[64] __align8;
00854 ScanTable scantable;
00855 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
00856
00857 int restart_interval;
00858 int restart_count;
00859
00860 int buggy_avid;
00861 int cs_itu601;
00862 int interlace_polarity;
00863
00864 int mjpb_skiptosod;
00865 } MJpegDecodeContext;
00866
00867 static int mjpeg_decode_dht(MJpegDecodeContext *s);
00868
00869 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
00870 int nb_codes, int use_static)
00871 {
00872 uint8_t huff_size[256];
00873 uint16_t huff_code[256];
00874
00875 memset(huff_size, 0, sizeof(huff_size));
00876 build_huffman_codes(huff_size, huff_code, bits_table, val_table);
00877
00878 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
00879 }
00880
00881 static int mjpeg_decode_init(AVCodecContext *avctx)
00882 {
00883 MJpegDecodeContext *s = avctx->priv_data;
00884 MpegEncContext s2;
00885 memset(s, 0, sizeof(MJpegDecodeContext));
00886
00887 s->avctx = avctx;
00888
00889
00890 memset(&s2, 0, sizeof(MpegEncContext));
00891 s2.avctx= avctx;
00892
00893 dsputil_init(&s2.dsp, avctx);
00894 DCT_common_init(&s2);
00895
00896 s->scantable= s2.intra_scantable;
00897 s->idct_put= s2.dsp.idct_put;
00898
00899 s->mpeg_enc_ctx_allocated = 0;
00900 s->buffer_size = 0;
00901 s->buffer = NULL;
00902 s->start_code = -1;
00903 s->first_picture = 1;
00904 s->org_height = avctx->coded_height;
00905
00906 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12, 0);
00907 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12, 0);
00908 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251, 0);
00909 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251, 0);
00910
00911 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
00912 {
00913 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
00914 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
00915 mjpeg_decode_dht(s);
00916
00917 }
00918
00919 return 0;
00920 }
00921
00922
00927 static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
00928 int vop_found, i;
00929 uint16_t state;
00930
00931 vop_found= pc->frame_start_found;
00932 state= pc->state;
00933
00934 i=0;
00935 if(!vop_found){
00936 for(i=0; i<buf_size; i++){
00937 state= (state<<8) | buf[i];
00938 if(state == 0xFFD8){
00939 i++;
00940 vop_found=1;
00941 break;
00942 }
00943 }
00944 }
00945
00946 if(vop_found){
00947
00948 if (buf_size == 0)
00949 return 0;
00950 for(; i<buf_size; i++){
00951 state= (state<<8) | buf[i];
00952 if(state == 0xFFD8){
00953 pc->frame_start_found=0;
00954 pc->state=0;
00955 return i-1;
00956 }
00957 }
00958 }
00959 pc->frame_start_found= vop_found;
00960 pc->state= state;
00961 return END_NOT_FOUND;
00962 }
00963
00964 static int jpeg_parse(AVCodecParserContext *s,
00965 AVCodecContext *avctx,
00966 uint8_t **poutbuf, int *poutbuf_size,
00967 const uint8_t *buf, int buf_size)
00968 {
00969 ParseContext *pc = s->priv_data;
00970 int next;
00971
00972 next= find_frame_end(pc, buf, buf_size);
00973
00974 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
00975 *poutbuf = NULL;
00976 *poutbuf_size = 0;
00977 return buf_size;
00978 }
00979
00980 *poutbuf = (uint8_t *)buf;
00981 *poutbuf_size = buf_size;
00982 return next;
00983 }
00984
00985
00986 static int mjpeg_decode_dqt(MJpegDecodeContext *s)
00987 {
00988 int len, index, i, j;
00989
00990 len = get_bits(&s->gb, 16) - 2;
00991
00992 while (len >= 65) {
00993
00994 if (get_bits(&s->gb, 4) != 0)
00995 {
00996 dprintf("dqt: 16bit precision\n");
00997 return -1;
00998 }
00999 index = get_bits(&s->gb, 4);
01000 if (index >= 4)
01001 return -1;
01002 dprintf("index=%d\n", index);
01003
01004 for(i=0;i<64;i++) {
01005 j = s->scantable.permutated[i];
01006 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
01007 }
01008
01009
01010 s->qscale[index]= FFMAX(
01011 s->quant_matrixes[index][s->scantable.permutated[1]],
01012 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
01013 dprintf("qscale[%d]: %d\n", index, s->qscale[index]);
01014 len -= 65;
01015 }
01016
01017 return 0;
01018 }
01019
01020
01021 static int mjpeg_decode_dht(MJpegDecodeContext *s)
01022 {
01023 int len, index, i, class, n, v, code_max;
01024 uint8_t bits_table[17];
01025 uint8_t val_table[256];
01026
01027 len = get_bits(&s->gb, 16) - 2;
01028
01029 while (len > 0) {
01030 if (len < 17)
01031 return -1;
01032 class = get_bits(&s->gb, 4);
01033 if (class >= 2)
01034 return -1;
01035 index = get_bits(&s->gb, 4);
01036 if (index >= 4)
01037 return -1;
01038 n = 0;
01039 for(i=1;i<=16;i++) {
01040 bits_table[i] = get_bits(&s->gb, 8);
01041 n += bits_table[i];
01042 }
01043 len -= 17;
01044 if (len < n || n > 256)
01045 return -1;
01046
01047 code_max = 0;
01048 for(i=0;i<n;i++) {
01049 v = get_bits(&s->gb, 8);
01050 if (v > code_max)
01051 code_max = v;
01052 val_table[i] = v;
01053 }
01054 len -= n;
01055
01056
01057 free_vlc(&s->vlcs[class][index]);
01058 dprintf("class=%d index=%d nb_codes=%d\n",
01059 class, index, code_max + 1);
01060 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0) < 0){
01061 return -1;
01062 }
01063 }
01064 return 0;
01065 }
01066
01067 static int mjpeg_decode_sof(MJpegDecodeContext *s)
01068 {
01069 int len, nb_components, i, width, height;
01070
01071
01072 len = get_bits(&s->gb, 16);
01073 s->bits= get_bits(&s->gb, 8);
01074
01075 if(s->pegasus_rct) s->bits=9;
01076 if(s->bits==9 && !s->pegasus_rct) s->rct=1;
01077
01078 if (s->bits != 8 && !s->lossless){
01079 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
01080 return -1;
01081 }
01082 height = get_bits(&s->gb, 16);
01083 width = get_bits(&s->gb, 16);
01084
01085 dprintf("sof0: picture: %dx%d\n", width, height);
01086 if(avcodec_check_dimensions(s->avctx, width, height))
01087 return -1;
01088
01089 nb_components = get_bits(&s->gb, 8);
01090 if (nb_components <= 0 ||
01091 nb_components > MAX_COMPONENTS)
01092 return -1;
01093 s->nb_components = nb_components;
01094 s->h_max = 1;
01095 s->v_max = 1;
01096 for(i=0;i<nb_components;i++) {
01097
01098 s->component_id[i] = get_bits(&s->gb, 8) - 1;
01099 s->h_count[i] = get_bits(&s->gb, 4);
01100 s->v_count[i] = get_bits(&s->gb, 4);
01101
01102 if (s->h_count[i] > s->h_max)
01103 s->h_max = s->h_count[i];
01104 if (s->v_count[i] > s->v_max)
01105 s->v_max = s->v_count[i];
01106 s->quant_index[i] = get_bits(&s->gb, 8);
01107 if (s->quant_index[i] >= 4)
01108 return -1;
01109 dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
01110 s->v_count[i], s->component_id[i], s->quant_index[i]);
01111 }
01112
01113 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
01114
01115
01116
01117 if (width != s->width || height != s->height) {
01118 av_freep(&s->qscale_table);
01119
01120 s->width = width;
01121 s->height = height;
01122 avcodec_set_dimensions(s->avctx, width, height);
01123
01124
01125 if (s->first_picture &&
01126 s->org_height != 0 &&
01127 s->height < ((s->org_height * 3) / 4)) {
01128 s->interlaced = 1;
01129
01130 s->bottom_field = 0;
01131 s->avctx->height *= 2;
01132 }
01133
01134 s->qscale_table= av_mallocz((s->width+15)/16);
01135
01136 s->first_picture = 0;
01137 }
01138
01139 if(s->interlaced && s->bottom_field)
01140 return 0;
01141
01142
01143 switch((s->h_count[0] << 4) | s->v_count[0]) {
01144 case 0x11:
01145 if(s->rgb){
01146 s->avctx->pix_fmt = PIX_FMT_RGBA32;
01147 }else if(s->nb_components==3)
01148 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
01149 else
01150 s->avctx->pix_fmt = PIX_FMT_GRAY8;
01151 break;
01152 case 0x21:
01153 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
01154 break;
01155 default:
01156 case 0x22:
01157 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
01158 break;
01159 }
01160
01161 if(s->picture.data[0])
01162 s->avctx->release_buffer(s->avctx, &s->picture);
01163
01164 s->picture.reference= 0;
01165 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
01166 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01167 return -1;
01168 }
01169 s->picture.pict_type= I_TYPE;
01170 s->picture.key_frame= 1;
01171
01172 for(i=0; i<3; i++){
01173 s->linesize[i]= s->picture.linesize[i] << s->interlaced;
01174 }
01175
01176
01177
01178 if (len != (8+(3*nb_components)))
01179 {
01180 dprintf("decode_sof0: error, len(%d) mismatch\n", len);
01181 }
01182
01183 return 0;
01184 }
01185
01186 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
01187 {
01188 int code;
01189 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
01190 if (code < 0)
01191 {
01192 dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
01193 &s->vlcs[0][dc_index]);
01194 return 0xffff;
01195 }
01196
01197 if(code)
01198 return get_xbits(&s->gb, code);
01199 else
01200 return 0;
01201 }
01202
01203
01204 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
01205 int component, int dc_index, int ac_index, int quant_index)
01206 {
01207 int code, i, j, level, val;
01208 VLC *ac_vlc;
01209 int16_t *quant_matrix;
01210
01211
01212 val = mjpeg_decode_dc(s, dc_index);
01213 if (val == 0xffff) {
01214 dprintf("error dc\n");
01215 return -1;
01216 }
01217 quant_matrix = s->quant_matrixes[quant_index];
01218 val = val * quant_matrix[0] + s->last_dc[component];
01219 s->last_dc[component] = val;
01220 block[0] = val;
01221
01222 ac_vlc = &s->vlcs[1][ac_index];
01223 i = 1;
01224 for(;;) {
01225 code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);
01226
01227 if (code < 0) {
01228 dprintf("error ac\n");
01229 return -1;
01230 }
01231
01232 if (code == 0)
01233 break;
01234 if (code == 0xf0) {
01235 i += 16;
01236 } else {
01237 level = get_xbits(&s->gb, code & 0xf);
01238 i += code >> 4;
01239 if (i >= 64) {
01240 dprintf("error count: %d\n", i);
01241 return -1;
01242 }
01243 j = s->scantable.permutated[i];
01244 block[j] = level * quant_matrix[j];
01245 i++;
01246 if (i >= 64)
01247 break;
01248 }
01249 }
01250 return 0;
01251 }
01252
01253 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
01254 int i, mb_x, mb_y;
01255 uint16_t buffer[32768][4];
01256 int left[3], top[3], topleft[3];
01257 const int linesize= s->linesize[0];
01258 const int mask= (1<<s->bits)-1;
01259
01260 if((unsigned)s->mb_width > 32768)
01261 return -1;
01262
01263 for(i=0; i<3; i++){
01264 buffer[0][i]= 1 << (s->bits + point_transform - 1);
01265 }
01266 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
01267 const int modified_predictor= mb_y ? predictor : 1;
01268 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
01269
01270 if (s->interlaced && s->bottom_field)
01271 ptr += linesize >> 1;
01272
01273 for(i=0; i<3; i++){
01274 top[i]= left[i]= topleft[i]= buffer[0][i];
01275 }
01276 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
01277 if (s->restart_interval && !s->restart_count)
01278 s->restart_count = s->restart_interval;
01279
01280 for(i=0;i<3;i++) {
01281 int pred;
01282
01283 topleft[i]= top[i];
01284 top[i]= buffer[mb_x][i];
01285
01286 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
01287
01288 left[i]=
01289 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
01290 }
01291
01292 if (s->restart_interval && !--s->restart_count) {
01293 align_get_bits(&s->gb);
01294 skip_bits(&s->gb, 16);
01295 }
01296 }
01297
01298 if(s->rct){
01299 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
01300 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
01301 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
01302 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
01303 }
01304 }else if(s->pegasus_rct){
01305 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
01306 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
01307 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
01308 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
01309 }
01310 }else{
01311 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
01312 ptr[4*mb_x+0] = buffer[mb_x][0];
01313 ptr[4*mb_x+1] = buffer[mb_x][1];
01314 ptr[4*mb_x+2] = buffer[mb_x][2];
01315 }
01316 }
01317 }
01318 return 0;
01319 }
01320
01321 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
01322 int i, mb_x, mb_y;
01323 const int nb_components=3;
01324
01325 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
01326 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
01327 if (s->restart_interval && !s->restart_count)
01328 s->restart_count = s->restart_interval;
01329
01330 if(mb_x==0 || mb_y==0 || s->interlaced){
01331 for(i=0;i<nb_components;i++) {
01332 uint8_t *ptr;
01333 int n, h, v, x, y, c, j, linesize;
01334 n = s->nb_blocks[i];
01335 c = s->comp_index[i];
01336 h = s->h_scount[i];
01337 v = s->v_scount[i];
01338 x = 0;
01339 y = 0;
01340 linesize= s->linesize[c];
01341
01342 for(j=0; j<n; j++) {
01343 int pred;
01344
01345 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
01346 if(y==0 && mb_y==0){
01347 if(x==0 && mb_x==0){
01348 pred= 128 << point_transform;
01349 }else{
01350 pred= ptr[-1];
01351 }
01352 }else{
01353 if(x==0 && mb_x==0){
01354 pred= ptr[-linesize];
01355 }else{
01356 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
01357 }
01358 }
01359
01360 if (s->interlaced && s->bottom_field)
01361 ptr += linesize >> 1;
01362 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
01363
01364 if (++x == h) {
01365 x = 0;
01366 y++;
01367 }
01368 }
01369 }
01370 }else{
01371 for(i=0;i<nb_components;i++) {
01372 uint8_t *ptr;
01373 int n, h, v, x, y, c, j, linesize;
01374 n = s->nb_blocks[i];
01375 c = s->comp_index[i];
01376 h = s->h_scount[i];
01377 v = s->v_scount[i];
01378 x = 0;
01379 y = 0;
01380 linesize= s->linesize[c];
01381
01382 for(j=0; j<n; j++) {
01383 int pred;
01384
01385 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x);
01386 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
01387 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
01388 if (++x == h) {
01389 x = 0;
01390 y++;
01391 }
01392 }
01393 }
01394 }
01395 if (s->restart_interval && !--s->restart_count) {
01396 align_get_bits(&s->gb);
01397 skip_bits(&s->gb, 16);
01398 }
01399 }
01400 }
01401 return 0;
01402 }
01403
01404 static int mjpeg_decode_scan(MJpegDecodeContext *s){
01405 int i, mb_x, mb_y;
01406 const int nb_components=3;
01407
01408 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
01409 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
01410 if (s->restart_interval && !s->restart_count)
01411 s->restart_count = s->restart_interval;
01412
01413 for(i=0;i<nb_components;i++) {
01414 uint8_t *ptr;
01415 int n, h, v, x, y, c, j;
01416 n = s->nb_blocks[i];
01417 c = s->comp_index[i];
01418 h = s->h_scount[i];
01419 v = s->v_scount[i];
01420 x = 0;
01421 y = 0;
01422 for(j=0;j<n;j++) {
01423 memset(s->block, 0, sizeof(s->block));
01424 if (decode_block(s, s->block, i,
01425 s->dc_index[i], s->ac_index[i],
01426 s->quant_index[c]) < 0) {
01427 dprintf("error y=%d x=%d\n", mb_y, mb_x);
01428 return -1;
01429 }
01430
01431 ptr = s->picture.data[c] +
01432 (((s->linesize[c] * (v * mb_y + y) * 8) +
01433 (h * mb_x + x) * 8) >> s->avctx->lowres);
01434 if (s->interlaced && s->bottom_field)
01435 ptr += s->linesize[c] >> 1;
01436
01437 s->idct_put(ptr, s->linesize[c], s->block);
01438 if (++x == h) {
01439 x = 0;
01440 y++;
01441 }
01442 }
01443 }
01444
01445 if (s->restart_interval && (s->restart_interval < 1350) &&
01446 !--s->restart_count) {
01447 align_get_bits(&s->gb);
01448 skip_bits(&s->gb, 16);
01449 for (i=0; i<nb_components; i++)
01450 s->last_dc[i] = 1024;
01451 }
01452 }
01453 }
01454 return 0;
01455 }
01456
01457 static int mjpeg_decode_sos(MJpegDecodeContext *s)
01458 {
01459 int len, nb_components, i, h, v, predictor, point_transform;
01460 int vmax, hmax, index, id;
01461 const int block_size= s->lossless ? 1 : 8;
01462
01463
01464 len = get_bits(&s->gb, 16);
01465 nb_components = get_bits(&s->gb, 8);
01466 if (len != 6+2*nb_components)
01467 {
01468 dprintf("decode_sos: invalid len (%d)\n", len);
01469 return -1;
01470 }
01471
01472 if (nb_components != s->nb_components)
01473 {
01474 dprintf("decode_sos: components(%d) mismatch\n", nb_components);
01475 return -1;
01476 }
01477 vmax = 0;
01478 hmax = 0;
01479 for(i=0;i<nb_components;i++) {
01480 id = get_bits(&s->gb, 8) - 1;
01481 dprintf("component: %d\n", id);
01482
01483 for(index=0;index<s->nb_components;index++)
01484 if (id == s->component_id[index])
01485 break;
01486 if (index == s->nb_components)
01487 {
01488 dprintf("decode_sos: index(%d) out of components\n", index);
01489 return -1;
01490 }
01491
01492 s->comp_index[i] = index;
01493
01494 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
01495 s->h_scount[i] = s->h_count[index];
01496 s->v_scount[i] = s->v_count[index];
01497
01498 s->dc_index[i] = get_bits(&s->gb, 4);
01499 s->ac_index[i] = get_bits(&s->gb, 4);
01500
01501 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
01502 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
01503 goto out_of_range;
01504 #if 0 //buggy
01505 switch(s->start_code)
01506 {
01507 case SOF0:
01508 if (dc_index[i] > 1 || ac_index[i] > 1)
01509 goto out_of_range;
01510 break;
01511 case SOF1:
01512 case SOF2:
01513 if (dc_index[i] > 3 || ac_index[i] > 3)
01514 goto out_of_range;
01515 break;
01516 case SOF3:
01517 if (dc_index[i] > 3 || ac_index[i] != 0)
01518 goto out_of_range;
01519 break;
01520 }
01521 #endif
01522 }
01523
01524 predictor= get_bits(&s->gb, 8);
01525 skip_bits(&s->gb, 8);
01526 skip_bits(&s->gb, 4);
01527 point_transform= get_bits(&s->gb, 4);
01528
01529 for(i=0;i<nb_components;i++)
01530 s->last_dc[i] = 1024;
01531
01532 if (nb_components > 1) {
01533
01534 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
01535 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
01536 } else {
01537 h = s->h_max / s->h_scount[s->comp_index[0]];
01538 v = s->v_max / s->v_scount[s->comp_index[0]];
01539 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
01540 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
01541 s->nb_blocks[0] = 1;
01542 s->h_scount[0] = 1;
01543 s->v_scount[0] = 1;
01544 }
01545
01546 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01547 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "", predictor, point_transform);
01548
01549
01550 for (i = s->mjpb_skiptosod; i > 0; i--)
01551 skip_bits(&s->gb, 8);
01552
01553 if(s->lossless){
01554 if(s->rgb){
01555 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
01556 return -1;
01557 }else{
01558 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
01559 return -1;
01560 }
01561 }else{
01562 if(mjpeg_decode_scan(s) < 0)
01563 return -1;
01564 }
01565 emms_c();
01566 return 0;
01567 out_of_range:
01568 dprintf("decode_sos: ac/dc index out of range\n");
01569 return -1;
01570 }
01571
01572 static int mjpeg_decode_dri(MJpegDecodeContext *s)
01573 {
01574 if (get_bits(&s->gb, 16) != 4)
01575 return -1;
01576 s->restart_interval = get_bits(&s->gb, 16);
01577 s->restart_count = 0;
01578 dprintf("restart interval: %d\n", s->restart_interval);
01579
01580 return 0;
01581 }
01582
01583 static int mjpeg_decode_app(MJpegDecodeContext *s)
01584 {
01585 int len, id;
01586
01587 len = get_bits(&s->gb, 16);
01588 if (len < 5)
01589 return -1;
01590 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
01591 return -1;
01592
01593 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
01594 id = be2me_32(id);
01595 len -= 6;
01596
01597 if(s->avctx->debug & FF_DEBUG_STARTCODE){
01598 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
01599 }
01600
01601
01602
01603
01604 if (id == ff_get_fourcc("AVI1"))
01605 {
01606
01607
01608
01609
01610
01611
01612
01613 s->buggy_avid = 1;
01614
01615
01616 s->interlace_polarity = get_bits(&s->gb, 8);
01617 #if 0
01618 skip_bits(&s->gb, 8);
01619 skip_bits(&s->gb, 32);
01620 skip_bits(&s->gb, 32);
01621 len -= 10;
01622 #endif
01623
01624
01625 goto out;
01626 }
01627
01628
01629
01630 if (id == ff_get_fourcc("JFIF"))
01631 {
01632 int t_w, t_h, v1, v2;
01633 skip_bits(&s->gb, 8);
01634 v1= get_bits(&s->gb, 8);
01635 v2= get_bits(&s->gb, 8);
01636 skip_bits(&s->gb, 8);
01637
01638 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
01639 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
01640
01641 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01642 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
01643 v1, v2,
01644 s->avctx->sample_aspect_ratio.num,
01645 s->avctx->sample_aspect_ratio.den
01646 );
01647
01648 t_w = get_bits(&s->gb, 8);
01649 t_h = get_bits(&s->gb, 8);
01650 if (t_w && t_h)
01651 {
01652
01653 if (len-10-(t_w*t_h*3) > 0)
01654 len -= t_w*t_h*3;
01655 }
01656 len -= 10;
01657 goto out;
01658 }
01659
01660 if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
01661 {
01662 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01663 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
01664 skip_bits(&s->gb, 16);
01665 skip_bits(&s->gb, 16);
01666 skip_bits(&s->gb, 16);
01667 skip_bits(&s->gb, 8);
01668 len -= 7;
01669 goto out;
01670 }
01671
01672 if (id == ff_get_fourcc("LJIF")){
01673 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01674 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
01675 skip_bits(&s->gb, 16);
01676 skip_bits(&s->gb, 16);
01677 skip_bits(&s->gb, 16);
01678 skip_bits(&s->gb, 16);
01679 switch( get_bits(&s->gb, 8)){
01680 case 1:
01681 s->rgb= 1;
01682 s->pegasus_rct=0;
01683 break;
01684 case 2:
01685 s->rgb= 1;
01686 s->pegasus_rct=1;
01687 break;
01688 default:
01689 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
01690 }
01691 len -= 9;
01692 goto out;
01693 }
01694
01695
01696 if ((s->start_code == APP1) && (len > (0x28 - 8)))
01697 {
01698 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
01699 id = be2me_32(id);
01700 len -= 4;
01701 if (id == ff_get_fourcc("mjpg"))
01702 {
01703 #if 0
01704 skip_bits(&s->gb, 32);
01705 skip_bits(&s->gb, 32);
01706 skip_bits(&s->gb, 32);
01707 skip_bits(&s->gb, 32);
01708 skip_bits(&s->gb, 32);
01709 skip_bits(&s->gb, 32);
01710 skip_bits(&s->gb, 32);
01711 skip_bits(&s->gb, 32);
01712 #endif
01713 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
01714 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
01715 }
01716 }
01717
01718 out:
01719
01720 if (len < 0)
01721 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
01722 while(--len > 0)
01723 skip_bits(&s->gb, 8);
01724
01725 return 0;
01726 }
01727
01728 static int mjpeg_decode_com(MJpegDecodeContext *s)
01729 {
01730 int len = get_bits(&s->gb, 16);
01731 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
01732 uint8_t *cbuf = av_malloc(len - 1);
01733 if (cbuf) {
01734 int i;
01735 for (i = 0; i < len - 2; i++)
01736 cbuf[i] = get_bits(&s->gb, 8);
01737 if (i > 0 && cbuf[i-1] == '\n')
01738 cbuf[i-1] = 0;
01739 else
01740 cbuf[i] = 0;
01741
01742 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01743 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
01744
01745
01746 if (!strcmp(cbuf, "AVID"))
01747 {
01748 s->buggy_avid = 1;
01749
01750
01751 }
01752 else if(!strcmp(cbuf, "CS=ITU601")){
01753 s->cs_itu601= 1;
01754 }
01755
01756 av_free(cbuf);
01757 }
01758 }
01759
01760 return 0;
01761 }
01762
01763 #if 0
01764 static int valid_marker_list[] =
01765 {
01766
01767 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01768 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01769 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01770 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01771 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01772 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01773 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01774 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01775 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01776 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01777 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01778 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
01779 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01780 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01781 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01782 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
01783 }
01784 #endif
01785
01786
01787
01788 static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
01789 {
01790 uint8_t *buf_ptr;
01791 unsigned int v, v2;
01792 int val;
01793 #ifdef DEBUG
01794 int skipped=0;
01795 #endif
01796
01797 buf_ptr = *pbuf_ptr;
01798 while (buf_ptr < buf_end) {
01799 v = *buf_ptr++;
01800 v2 = *buf_ptr;
01801 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
01802 val = *buf_ptr++;
01803 goto found;
01804 }
01805 #ifdef DEBUG
01806 skipped++;
01807 #endif
01808 }
01809 val = -1;
01810 found:
01811 #ifdef DEBUG
01812 dprintf("find_marker skipped %d bytes\n", skipped);
01813 #endif
01814 *pbuf_ptr = buf_ptr;
01815 return val;
01816 }
01817
01818 static int mjpeg_decode_frame(AVCodecContext *avctx,
01819 void *data, int *data_size,
01820 uint8_t *buf, int buf_size)
01821 {
01822 MJpegDecodeContext *s = avctx->priv_data;
01823 uint8_t *buf_end, *buf_ptr;
01824 int start_code;
01825 AVFrame *picture = data;
01826
01827 buf_ptr = buf;
01828 buf_end = buf + buf_size;
01829 while (buf_ptr < buf_end) {
01830
01831 start_code = find_marker(&buf_ptr, buf_end);
01832 {
01833
01834 if (start_code < 0) {
01835 goto the_end;
01836 } else {
01837 dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
01838
01839 if ((buf_end - buf_ptr) > s->buffer_size)
01840 {
01841 av_free(s->buffer);
01842 s->buffer_size = buf_end-buf_ptr;
01843 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
01844 dprintf("buffer too small, expanding to %d bytes\n",
01845 s->buffer_size);
01846 }
01847
01848
01849 if (start_code == SOS)
01850 {
01851 uint8_t *src = buf_ptr;
01852 uint8_t *dst = s->buffer;
01853
01854 while (src<buf_end)
01855 {
01856 uint8_t x = *(src++);
01857
01858 *(dst++) = x;
01859 if (x == 0xff)
01860 {
01861 while(src<buf_end && x == 0xff)
01862 x = *(src++);
01863
01864 if (x >= 0xd0 && x <= 0xd7)
01865 *(dst++) = x;
01866 else if (x)
01867 break;
01868 }
01869 }
01870 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
01871
01872 dprintf("escaping removed %d bytes\n",
01873 (buf_end - buf_ptr) - (dst - s->buffer));
01874 }
01875 else
01876 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
01877
01878 s->start_code = start_code;
01879 if(s->avctx->debug & FF_DEBUG_STARTCODE){
01880 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
01881 }
01882
01883
01884 if (start_code >= 0xd0 && start_code <= 0xd7) {
01885 dprintf("restart marker: %d\n", start_code&0x0f);
01886
01887 } else if (start_code >= APP0 && start_code <= APP15) {
01888 mjpeg_decode_app(s);
01889
01890 } else if (start_code == COM){
01891 mjpeg_decode_com(s);
01892 }
01893
01894 switch(start_code) {
01895 case SOI:
01896 s->restart_interval = 0;
01897 s->restart_count = 0;
01898
01899 break;
01900 case DQT:
01901 mjpeg_decode_dqt(s);
01902 break;
01903 case DHT:
01904 if(mjpeg_decode_dht(s) < 0){
01905 av_log(s->avctx, AV_LOG_ERROR, "huffman table decode error\n");
01906 return -1;
01907 }
01908 break;
01909 case SOF0:
01910 s->lossless=0;
01911 if (mjpeg_decode_sof(s) < 0)
01912 return -1;
01913 break;
01914 case SOF3:
01915 s->lossless=1;
01916 if (mjpeg_decode_sof(s) < 0)
01917 return -1;
01918 break;
01919 case EOI:
01920 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
01921 break;
01922 eoi_parser:
01923 {
01924 if (s->interlaced) {
01925 s->bottom_field ^= 1;
01926
01927 if (s->bottom_field)
01928 goto not_the_end;
01929 }
01930 *picture = s->picture;
01931 *data_size = sizeof(AVFrame);
01932
01933 if(!s->lossless){
01934 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
01935 picture->qstride= 0;
01936 picture->qscale_table= s->qscale_table;
01937 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
01938 if(avctx->debug & FF_DEBUG_QP)
01939 av_log(s->avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
01940 picture->quality*= FF_QP2LAMBDA;
01941 }
01942
01943 goto the_end;
01944 }
01945 break;
01946 case SOS:
01947 mjpeg_decode_sos(s);
01948
01949
01950 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
01951 goto eoi_parser;
01952 break;
01953 case DRI:
01954 mjpeg_decode_dri(s);
01955 break;
01956 case SOF1:
01957 case SOF2:
01958 case SOF5:
01959 case SOF6:
01960 case SOF7:
01961 case SOF9:
01962 case SOF10:
01963 case SOF11:
01964 case SOF13:
01965 case SOF14:
01966 case SOF15:
01967 case JPG:
01968 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
01969 break;
01970
01971
01972
01973 }
01974
01975 not_the_end:
01976
01977 buf_ptr += (get_bits_count(&s->gb)+7)/8;
01978 dprintf("marker parser used %d bytes (%d bits)\n",
01979 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
01980 }
01981 }
01982 }
01983 the_end:
01984 dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
01985
01986 return buf_ptr - buf;
01987 }
01988
01989 static int mjpegb_decode_frame(AVCodecContext *avctx,
01990 void *data, int *data_size,
01991 uint8_t *buf, int buf_size)
01992 {
01993 MJpegDecodeContext *s = avctx->priv_data;
01994 uint8_t *buf_end, *buf_ptr;
01995 AVFrame *picture = data;
01996 GetBitContext hgb;
01997 uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
01998 uint32_t field_size, sod_offs;
01999
02000 buf_ptr = buf;
02001 buf_end = buf + buf_size;
02002
02003 read_header:
02004
02005 s->restart_interval = 0;
02006 s->restart_count = 0;
02007 s->mjpb_skiptosod = 0;
02008
02009 init_get_bits(&hgb, buf_ptr, (buf_end - buf_ptr)*8);
02010
02011 skip_bits(&hgb, 32);
02012
02013 if (get_bits_long(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
02014 {
02015 dprintf("not mjpeg-b (bad fourcc)\n");
02016 return 0;
02017 }
02018
02019 field_size = get_bits_long(&hgb, 32);
02020 dprintf("field size: 0x%x\n", field_size);
02021 skip_bits(&hgb, 32);
02022 second_field_offs = get_bits_long(&hgb, 32);
02023 dprintf("second field offs: 0x%x\n", second_field_offs);
02024 if (second_field_offs)
02025 s->interlaced = 1;
02026
02027 dqt_offs = get_bits_long(&hgb, 32);
02028 dprintf("dqt offs: 0x%x\n", dqt_offs);
02029 if (dqt_offs)
02030 {
02031 init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
02032 s->start_code = DQT;
02033 mjpeg_decode_dqt(s);
02034 }
02035
02036 dht_offs = get_bits_long(&hgb, 32);
02037 dprintf("dht offs: 0x%x\n", dht_offs);
02038 if (dht_offs)
02039 {
02040 init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
02041 s->start_code = DHT;
02042 mjpeg_decode_dht(s);
02043 }
02044
02045 sof_offs = get_bits_long(&hgb, 32);
02046 dprintf("sof offs: 0x%x\n", sof_offs);
02047 if (sof_offs)
02048 {
02049 init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
02050 s->start_code = SOF0;
02051 if (mjpeg_decode_sof(s) < 0)
02052 return -1;
02053 }
02054
02055 sos_offs = get_bits_long(&hgb, 32);
02056 dprintf("sos offs: 0x%x\n", sos_offs);
02057 sod_offs = get_bits_long(&hgb, 32);
02058 dprintf("sod offs: 0x%x\n", sod_offs);
02059 if (sos_offs)
02060 {
02061
02062 init_get_bits(&s->gb, buf+sos_offs, field_size*8);
02063 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
02064 s->start_code = SOS;
02065 mjpeg_decode_sos(s);
02066 }
02067
02068 if (s->interlaced) {
02069 s->bottom_field ^= 1;
02070
02071 if (s->bottom_field && second_field_offs)
02072 {
02073 buf_ptr = buf + second_field_offs;
02074 second_field_offs = 0;
02075 goto read_header;
02076 }
02077 }
02078
02079
02080
02081 *picture= s->picture;
02082 *data_size = sizeof(AVFrame);
02083
02084 if(!s->lossless){
02085 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
02086 picture->qstride= 0;
02087 picture->qscale_table= s->qscale_table;
02088 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
02089 if(avctx->debug & FF_DEBUG_QP)
02090 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
02091 picture->quality*= FF_QP2LAMBDA;
02092 }
02093
02094 return buf_ptr - buf;
02095 }
02096
02097 #include "sp5x.h"
02098
02099 static int sp5x_decode_frame(AVCodecContext *avctx,
02100 void *data, int *data_size,
02101 uint8_t *buf, int buf_size)
02102 {
02103 #if 0
02104 MJpegDecodeContext *s = avctx->priv_data;
02105 #endif
02106 const int qscale = 5;
02107 uint8_t *buf_ptr, *buf_end, *recoded;
02108 int i = 0, j = 0;
02109
02110 if (!avctx->width || !avctx->height)
02111 return -1;
02112
02113 buf_ptr = buf;
02114 buf_end = buf + buf_size;
02115
02116 #if 1
02117 recoded = av_mallocz(buf_size + 1024);
02118 if (!recoded)
02119 return -1;
02120
02121
02122 recoded[j++] = 0xFF;
02123 recoded[j++] = 0xD8;
02124
02125 memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
02126 memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
02127 memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
02128 j += sizeof(sp5x_data_dqt);
02129
02130 memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
02131 j += sizeof(sp5x_data_dht);
02132
02133 memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
02134 recoded[j+5] = (avctx->coded_height >> 8) & 0xFF;
02135 recoded[j+6] = avctx->coded_height & 0xFF;
02136 recoded[j+7] = (avctx->coded_width >> 8) & 0xFF;
02137 recoded[j+8] = avctx->coded_width & 0xFF;
02138 j += sizeof(sp5x_data_sof);
02139
02140 memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
02141 j += sizeof(sp5x_data_sos);
02142
02143 for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
02144 {
02145 recoded[j++] = buf[i];
02146 if (buf[i] == 0xff)
02147 recoded[j++] = 0;
02148 }
02149
02150
02151 recoded[j++] = 0xFF;
02152 recoded[j++] = 0xD9;
02153
02154 i = mjpeg_decode_frame(avctx, data, data_size, recoded, j);
02155
02156 av_free(recoded);
02157
02158 #else
02159
02160 s->bits = 8;
02161 s->width = avctx->coded_width;
02162 s->height = avctx->coded_height;
02163 s->nb_components = 3;
02164 s->component_id[0] = 0;
02165 s->h_count[0] = 2;
02166 s->v_count[0] = 2;
02167 s->quant_index[0] = 0;
02168 s->component_id[1] = 1;
02169 s->h_count[1] = 1;
02170 s->v_count[1] = 1;
02171 s->quant_index[1] = 1;
02172 s->component_id[2] = 2;
02173 s->h_count[2] = 1;
02174 s->v_count[2] = 1;
02175 s->quant_index[2] = 1;
02176 s->h_max = 2;
02177 s->v_max = 2;
02178
02179 s->qscale_table = av_mallocz((s->width+15)/16);
02180 avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
02181 s->interlaced = 0;
02182
02183 s->picture.reference = 0;
02184 if (avctx->get_buffer(avctx, &s->picture) < 0)
02185 {
02186 fprintf(stderr, "get_buffer() failed\n");
02187 return -1;
02188 }
02189
02190 s->picture.pict_type = I_TYPE;
02191 s->picture.key_frame = 1;
02192
02193 for (i = 0; i < 3; i++)
02194 s->linesize[i] = s->picture.linesize[i] << s->interlaced;
02195
02196
02197 for (i = 0; i < 64; i++)
02198 {
02199 j = s->scantable.permutated[i];
02200 s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
02201 }
02202 s->qscale[0] = FFMAX(
02203 s->quant_matrixes[0][s->scantable.permutated[1]],
02204 s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
02205
02206 for (i = 0; i < 64; i++)
02207 {
02208 j = s->scantable.permutated[i];
02209 s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
02210 }
02211 s->qscale[1] = FFMAX(
02212 s->quant_matrixes[1][s->scantable.permutated[1]],
02213 s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
02214
02215
02216
02217
02218 s->comp_index[0] = 0;
02219 s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
02220 s->h_scount[0] = s->h_count[0];
02221 s->v_scount[0] = s->v_count[0];
02222 s->dc_index[0] = 0;
02223 s->ac_index[0] = 0;
02224
02225 s->comp_index[1] = 1;
02226 s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
02227 s->h_scount[1] = s->h_count[1];
02228 s->v_scount[1] = s->v_count[1];
02229 s->dc_index[1] = 1;
02230 s->ac_index[1] = 1;
02231
02232 s->comp_index[2] = 2;
02233 s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
02234 s->h_scount[2] = s->h_count[2];
02235 s->v_scount[2] = s->v_count[2];
02236 s->dc_index[2] = 1;
02237 s->ac_index[2] = 1;
02238
02239 for (i = 0; i < 3; i++)
02240 s->last_dc[i] = 1024;
02241
02242 s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
02243 s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
02244
02245 init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
02246
02247 return mjpeg_decode_scan(s);
02248 #endif
02249
02250 return i;
02251 }
02252
02253 static int mjpeg_decode_end(AVCodecContext *avctx)
02254 {
02255 MJpegDecodeContext *s = avctx->priv_data;
02256 int i, j;
02257
02258 av_free(s->buffer);
02259 av_free(s->qscale_table);
02260
02261 for(i=0;i<2;i++) {
02262 for(j=0;j<4;j++)
02263 free_vlc(&s->vlcs[i][j]);
02264 }
02265 return 0;
02266 }
02267
02268 AVCodec mjpeg_decoder = {
02269 "mjpeg",
02270 CODEC_TYPE_VIDEO,
02271 CODEC_ID_MJPEG,
02272 sizeof(MJpegDecodeContext),
02273 mjpeg_decode_init,
02274 NULL,
02275 mjpeg_decode_end,
02276 mjpeg_decode_frame,
02277 CODEC_CAP_DR1,
02278 NULL
02279 };
02280
02281 AVCodec mjpegb_decoder = {
02282 "mjpegb",
02283 CODEC_TYPE_VIDEO,
02284 CODEC_ID_MJPEGB,
02285 sizeof(MJpegDecodeContext),
02286 mjpeg_decode_init,
02287 NULL,
02288 mjpeg_decode_end,
02289 mjpegb_decode_frame,
02290 CODEC_CAP_DR1,
02291 NULL
02292 };
02293
02294 AVCodec sp5x_decoder = {
02295 "sp5x",
02296 CODEC_TYPE_VIDEO,
02297 CODEC_ID_SP5X,
02298 sizeof(MJpegDecodeContext),
02299 mjpeg_decode_init,
02300 NULL,
02301 mjpeg_decode_end,
02302 sp5x_decode_frame,
02303 CODEC_CAP_DR1,
02304 NULL
02305 };
02306
02307 #ifdef CONFIG_ENCODERS
02308 AVCodec ljpeg_encoder = {
02309 "ljpeg",
02310 CODEC_TYPE_VIDEO,
02311 CODEC_ID_LJPEG,
02312 sizeof(MpegEncContext),
02313 MPV_encode_init,
02314 encode_picture_lossless,
02315 MPV_encode_end,
02316 };
02317 #endif
02318
02319 AVCodecParser mjpeg_parser = {
02320 { CODEC_ID_MJPEG },
02321 sizeof(ParseContext),
02322 NULL,
02323 jpeg_parse,
02324 ff_parse_close,
02325 };
02326