• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/mjpegenc.c

Go to the documentation of this file.
00001 /*
00002  * MJPEG encoder
00003  * Copyright (c) 2000, 2001 Fabrice Bellard
00004  * Copyright (c) 2003 Alex Beregszaszi
00005  * Copyright (c) 2003-2004 Michael Niedermayer
00006  *
00007  * Support for external huffman table, various fixes (AVID workaround),
00008  * aspecting, new decode_frame mechanism and apple mjpeg-b support
00009  *                                  by Alex Beregszaszi
00010  *
00011  * This file is part of FFmpeg.
00012  *
00013  * FFmpeg is free software; you can redistribute it and/or
00014  * modify it under the terms of the GNU Lesser General Public
00015  * License as published by the Free Software Foundation; either
00016  * version 2.1 of the License, or (at your option) any later version.
00017  *
00018  * FFmpeg is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021  * Lesser General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public
00024  * License along with FFmpeg; if not, write to the Free Software
00025  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00026  */
00027 
00033 //#define DEBUG
00034 #include <assert.h>
00035 
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 #include "mpegvideo.h"
00039 #include "mjpeg.h"
00040 #include "mjpegenc.h"
00041 
00042 /* use two quantizer tables (one for luminance and one for chrominance) */
00043 /* not yet working */
00044 #undef TWOMATRIXES
00045 
00046 
00047 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
00048 {
00049     MJpegContext *m;
00050 
00051     m = av_malloc(sizeof(MJpegContext));
00052     if (!m)
00053         return -1;
00054 
00055     s->min_qcoeff=-1023;
00056     s->max_qcoeff= 1023;
00057 
00058     /* build all the huffman tables */
00059     ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
00060                                  m->huff_code_dc_luminance,
00061                                  ff_mjpeg_bits_dc_luminance,
00062                                  ff_mjpeg_val_dc);
00063     ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
00064                                  m->huff_code_dc_chrominance,
00065                                  ff_mjpeg_bits_dc_chrominance,
00066                                  ff_mjpeg_val_dc);
00067     ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
00068                                  m->huff_code_ac_luminance,
00069                                  ff_mjpeg_bits_ac_luminance,
00070                                  ff_mjpeg_val_ac_luminance);
00071     ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
00072                                  m->huff_code_ac_chrominance,
00073                                  ff_mjpeg_bits_ac_chrominance,
00074                                  ff_mjpeg_val_ac_chrominance);
00075 
00076     s->mjpeg_ctx = m;
00077     return 0;
00078 }
00079 
00080 void ff_mjpeg_encode_close(MpegEncContext *s)
00081 {
00082     av_free(s->mjpeg_ctx);
00083 }
00084 
00085 /* table_class: 0 = DC coef, 1 = AC coefs */
00086 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
00087                              const uint8_t *bits_table, const uint8_t *value_table)
00088 {
00089     PutBitContext *p = &s->pb;
00090     int n, i;
00091 
00092     put_bits(p, 4, table_class);
00093     put_bits(p, 4, table_id);
00094 
00095     n = 0;
00096     for(i=1;i<=16;i++) {
00097         n += bits_table[i];
00098         put_bits(p, 8, bits_table[i]);
00099     }
00100 
00101     for(i=0;i<n;i++)
00102         put_bits(p, 8, value_table[i]);
00103 
00104     return n + 17;
00105 }
00106 
00107 static void jpeg_table_header(MpegEncContext *s)
00108 {
00109     PutBitContext *p = &s->pb;
00110     int i, j, size;
00111     uint8_t *ptr;
00112 
00113     /* quant matrixes */
00114     put_marker(p, DQT);
00115 #ifdef TWOMATRIXES
00116     put_bits(p, 16, 2 + 2 * (1 + 64));
00117 #else
00118     put_bits(p, 16, 2 + 1 * (1 + 64));
00119 #endif
00120     put_bits(p, 4, 0); /* 8 bit precision */
00121     put_bits(p, 4, 0); /* table 0 */
00122     for(i=0;i<64;i++) {
00123         j = s->intra_scantable.permutated[i];
00124         put_bits(p, 8, s->intra_matrix[j]);
00125     }
00126 #ifdef TWOMATRIXES
00127     put_bits(p, 4, 0); /* 8 bit precision */
00128     put_bits(p, 4, 1); /* table 1 */
00129     for(i=0;i<64;i++) {
00130         j = s->intra_scantable.permutated[i];
00131         put_bits(p, 8, s->chroma_intra_matrix[j]);
00132     }
00133 #endif
00134 
00135     /* huffman table */
00136     put_marker(p, DHT);
00137     flush_put_bits(p);
00138     ptr = pbBufPtr(p);
00139     put_bits(p, 16, 0); /* patched later */
00140     size = 2;
00141     size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
00142                               ff_mjpeg_val_dc);
00143     size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
00144                               ff_mjpeg_val_dc);
00145 
00146     size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
00147                               ff_mjpeg_val_ac_luminance);
00148     size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
00149                               ff_mjpeg_val_ac_chrominance);
00150     AV_WB16(ptr, size);
00151 }
00152 
00153 static void jpeg_put_comments(MpegEncContext *s)
00154 {
00155     PutBitContext *p = &s->pb;
00156     int size;
00157     uint8_t *ptr;
00158 
00159     if (s->aspect_ratio_info /* && !lossless */)
00160     {
00161     /* JFIF header */
00162     put_marker(p, APP0);
00163     put_bits(p, 16, 16);
00164     ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
00165     put_bits(p, 16, 0x0201); /* v 1.02 */
00166     put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
00167     put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
00168     put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
00169     put_bits(p, 8, 0); /* thumbnail width */
00170     put_bits(p, 8, 0); /* thumbnail height */
00171     }
00172 
00173     /* comment */
00174     if(!(s->flags & CODEC_FLAG_BITEXACT)){
00175         put_marker(p, COM);
00176         flush_put_bits(p);
00177         ptr = pbBufPtr(p);
00178         put_bits(p, 16, 0); /* patched later */
00179         ff_put_string(p, LIBAVCODEC_IDENT, 1);
00180         size = strlen(LIBAVCODEC_IDENT)+3;
00181         AV_WB16(ptr, size);
00182     }
00183 
00184     if(  s->avctx->pix_fmt == PIX_FMT_YUV420P
00185        ||s->avctx->pix_fmt == PIX_FMT_YUV422P
00186        ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
00187         put_marker(p, COM);
00188         flush_put_bits(p);
00189         ptr = pbBufPtr(p);
00190         put_bits(p, 16, 0); /* patched later */
00191         ff_put_string(p, "CS=ITU601", 1);
00192         size = strlen("CS=ITU601")+3;
00193         AV_WB16(ptr, size);
00194     }
00195 }
00196 
00197 void ff_mjpeg_encode_picture_header(MpegEncContext *s)
00198 {
00199     const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
00200 
00201     put_marker(&s->pb, SOI);
00202 
00203     jpeg_put_comments(s);
00204 
00205     jpeg_table_header(s);
00206 
00207     switch(s->avctx->codec_id){
00208     case CODEC_ID_MJPEG:  put_marker(&s->pb, SOF0 ); break;
00209     case CODEC_ID_LJPEG:  put_marker(&s->pb, SOF3 ); break;
00210     default: assert(0);
00211     }
00212 
00213     put_bits(&s->pb, 16, 17);
00214     if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32)
00215         put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
00216     else
00217         put_bits(&s->pb, 8, 8); /* 8 bits/component */
00218     put_bits(&s->pb, 16, s->height);
00219     put_bits(&s->pb, 16, s->width);
00220     put_bits(&s->pb, 8, 3); /* 3 components */
00221 
00222     /* Y component */
00223     put_bits(&s->pb, 8, 1); /* component number */
00224     put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
00225     put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
00226     put_bits(&s->pb, 8, 0); /* select matrix */
00227 
00228     /* Cb component */
00229     put_bits(&s->pb, 8, 2); /* component number */
00230     put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
00231     put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
00232 #ifdef TWOMATRIXES
00233     put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
00234 #else
00235     put_bits(&s->pb, 8, 0); /* select matrix */
00236 #endif
00237 
00238     /* Cr component */
00239     put_bits(&s->pb, 8, 3); /* component number */
00240     put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
00241     put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
00242 #ifdef TWOMATRIXES
00243     put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
00244 #else
00245     put_bits(&s->pb, 8, 0); /* select matrix */
00246 #endif
00247 
00248     /* scan header */
00249     put_marker(&s->pb, SOS);
00250     put_bits(&s->pb, 16, 12); /* length */
00251     put_bits(&s->pb, 8, 3); /* 3 components */
00252 
00253     /* Y component */
00254     put_bits(&s->pb, 8, 1); /* index */
00255     put_bits(&s->pb, 4, 0); /* DC huffman table index */
00256     put_bits(&s->pb, 4, 0); /* AC huffman table index */
00257 
00258     /* Cb component */
00259     put_bits(&s->pb, 8, 2); /* index */
00260     put_bits(&s->pb, 4, 1); /* DC huffman table index */
00261     put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
00262 
00263     /* Cr component */
00264     put_bits(&s->pb, 8, 3); /* index */
00265     put_bits(&s->pb, 4, 1); /* DC huffman table index */
00266     put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
00267 
00268     put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
00269 
00270     switch(s->avctx->codec_id){
00271     case CODEC_ID_MJPEG:  put_bits(&s->pb, 8, 63); break; /* Se (not used) */
00272     case CODEC_ID_LJPEG:  put_bits(&s->pb, 8,  0); break; /* not used */
00273     default: assert(0);
00274     }
00275 
00276     put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
00277 }
00278 
00279 static void escape_FF(MpegEncContext *s, int start)
00280 {
00281     int size= put_bits_count(&s->pb) - start*8;
00282     int i, ff_count;
00283     uint8_t *buf= s->pb.buf + start;
00284     int align= (-(size_t)(buf))&3;
00285 
00286     assert((size&7) == 0);
00287     size >>= 3;
00288 
00289     ff_count=0;
00290     for(i=0; i<size && i<align; i++){
00291         if(buf[i]==0xFF) ff_count++;
00292     }
00293     for(; i<size-15; i+=16){
00294         int acc, v;
00295 
00296         v= *(uint32_t*)(&buf[i]);
00297         acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00298         v= *(uint32_t*)(&buf[i+4]);
00299         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00300         v= *(uint32_t*)(&buf[i+8]);
00301         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00302         v= *(uint32_t*)(&buf[i+12]);
00303         acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
00304 
00305         acc>>=4;
00306         acc+= (acc>>16);
00307         acc+= (acc>>8);
00308         ff_count+= acc&0xFF;
00309     }
00310     for(; i<size; i++){
00311         if(buf[i]==0xFF) ff_count++;
00312     }
00313 
00314     if(ff_count==0) return;
00315 
00316     /* skip put bits */
00317     for(i=0; i<ff_count-3; i+=4)
00318         put_bits(&s->pb, 32, 0);
00319     put_bits(&s->pb, (ff_count-i)*8, 0);
00320     flush_put_bits(&s->pb);
00321 
00322     for(i=size-1; ff_count; i--){
00323         int v= buf[i];
00324 
00325         if(v==0xFF){
00326 //printf("%d %d\n", i, ff_count);
00327             buf[i+ff_count]= 0;
00328             ff_count--;
00329         }
00330 
00331         buf[i+ff_count]= v;
00332     }
00333 }
00334 
00335 void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
00336 {
00337     int length;
00338     length= (-put_bits_count(pbc))&7;
00339     if(length) put_bits(pbc, length, (1<<length)-1);
00340 }
00341 
00342 void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
00343 {
00344     ff_mjpeg_encode_stuffing(&s->pb);
00345     flush_put_bits(&s->pb);
00346 
00347     assert((s->header_bits&7)==0);
00348 
00349     escape_FF(s, s->header_bits>>3);
00350 
00351     put_marker(&s->pb, EOI);
00352 }
00353 
00354 void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
00355                         uint8_t *huff_size, uint16_t *huff_code)
00356 {
00357     int mant, nbits;
00358 
00359     if (val == 0) {
00360         put_bits(&s->pb, huff_size[0], huff_code[0]);
00361     } else {
00362         mant = val;
00363         if (val < 0) {
00364             val = -val;
00365             mant--;
00366         }
00367 
00368         nbits= av_log2_16bit(val) + 1;
00369 
00370         put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
00371 
00372         put_sbits(&s->pb, nbits, mant);
00373     }
00374 }
00375 
00376 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
00377 {
00378     int mant, nbits, code, i, j;
00379     int component, dc, run, last_index, val;
00380     MJpegContext *m = s->mjpeg_ctx;
00381     uint8_t *huff_size_ac;
00382     uint16_t *huff_code_ac;
00383 
00384     /* DC coef */
00385     component = (n <= 3 ? 0 : (n&1) + 1);
00386     dc = block[0]; /* overflow is impossible */
00387     val = dc - s->last_dc[component];
00388     if (n < 4) {
00389         ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
00390         huff_size_ac = m->huff_size_ac_luminance;
00391         huff_code_ac = m->huff_code_ac_luminance;
00392     } else {
00393         ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
00394         huff_size_ac = m->huff_size_ac_chrominance;
00395         huff_code_ac = m->huff_code_ac_chrominance;
00396     }
00397     s->last_dc[component] = dc;
00398 
00399     /* AC coefs */
00400 
00401     run = 0;
00402     last_index = s->block_last_index[n];
00403     for(i=1;i<=last_index;i++) {
00404         j = s->intra_scantable.permutated[i];
00405         val = block[j];
00406         if (val == 0) {
00407             run++;
00408         } else {
00409             while (run >= 16) {
00410                 put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
00411                 run -= 16;
00412             }
00413             mant = val;
00414             if (val < 0) {
00415                 val = -val;
00416                 mant--;
00417             }
00418 
00419             nbits= av_log2(val) + 1;
00420             code = (run << 4) | nbits;
00421 
00422             put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
00423 
00424             put_sbits(&s->pb, nbits, mant);
00425             run = 0;
00426         }
00427     }
00428 
00429     /* output EOB only if not already 64 values */
00430     if (last_index < 63 || run != 0)
00431         put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
00432 }
00433 
00434 void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
00435 {
00436     int i;
00437     for(i=0;i<5;i++) {
00438         encode_block(s, block[i], i);
00439     }
00440     if (s->chroma_format == CHROMA_420) {
00441         encode_block(s, block[5], 5);
00442     } else {
00443         encode_block(s, block[6], 6);
00444         encode_block(s, block[5], 5);
00445         encode_block(s, block[7], 7);
00446     }
00447 }
00448 
00449 AVCodec mjpeg_encoder = {
00450     "mjpeg",
00451     CODEC_TYPE_VIDEO,
00452     CODEC_ID_MJPEG,
00453     sizeof(MpegEncContext),
00454     MPV_encode_init,
00455     MPV_encode_picture,
00456     MPV_encode_end,
00457     .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE},
00458     .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
00459 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1