You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.7KB

  1. /*
  2. * MJPEG encoder and decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Support for external huffman table, various fixes (AVID workaround),
  24. * aspecting, new decode_frame mechanism and apple mjpeg-b support
  25. * by Alex Beregszaszi
  26. */
  27. /**
  28. * @file mjpeg.h
  29. * MJPEG encoder and decoder.
  30. */
  31. #ifndef MJPEG_H
  32. #define MJPEG_H
  33. #include "avcodec.h"
  34. #include "bitstream.h"
  35. #include "dsputil.h"
  36. #include "mpegvideo.h"
  37. /* JPEG marker codes */
  38. typedef enum {
  39. /* start of frame */
  40. SOF0 = 0xc0, /* baseline */
  41. SOF1 = 0xc1, /* extended sequential, huffman */
  42. SOF2 = 0xc2, /* progressive, huffman */
  43. SOF3 = 0xc3, /* lossless, huffman */
  44. SOF5 = 0xc5, /* differential sequential, huffman */
  45. SOF6 = 0xc6, /* differential progressive, huffman */
  46. SOF7 = 0xc7, /* differential lossless, huffman */
  47. JPG = 0xc8, /* reserved for JPEG extension */
  48. SOF9 = 0xc9, /* extended sequential, arithmetic */
  49. SOF10 = 0xca, /* progressive, arithmetic */
  50. SOF11 = 0xcb, /* lossless, arithmetic */
  51. SOF13 = 0xcd, /* differential sequential, arithmetic */
  52. SOF14 = 0xce, /* differential progressive, arithmetic */
  53. SOF15 = 0xcf, /* differential lossless, arithmetic */
  54. DHT = 0xc4, /* define huffman tables */
  55. DAC = 0xcc, /* define arithmetic-coding conditioning */
  56. /* restart with modulo 8 count "m" */
  57. RST0 = 0xd0,
  58. RST1 = 0xd1,
  59. RST2 = 0xd2,
  60. RST3 = 0xd3,
  61. RST4 = 0xd4,
  62. RST5 = 0xd5,
  63. RST6 = 0xd6,
  64. RST7 = 0xd7,
  65. SOI = 0xd8, /* start of image */
  66. EOI = 0xd9, /* end of image */
  67. SOS = 0xda, /* start of scan */
  68. DQT = 0xdb, /* define quantization tables */
  69. DNL = 0xdc, /* define number of lines */
  70. DRI = 0xdd, /* define restart interval */
  71. DHP = 0xde, /* define hierarchical progression */
  72. EXP = 0xdf, /* expand reference components */
  73. APP0 = 0xe0,
  74. APP1 = 0xe1,
  75. APP2 = 0xe2,
  76. APP3 = 0xe3,
  77. APP4 = 0xe4,
  78. APP5 = 0xe5,
  79. APP6 = 0xe6,
  80. APP7 = 0xe7,
  81. APP8 = 0xe8,
  82. APP9 = 0xe9,
  83. APP10 = 0xea,
  84. APP11 = 0xeb,
  85. APP12 = 0xec,
  86. APP13 = 0xed,
  87. APP14 = 0xee,
  88. APP15 = 0xef,
  89. JPG0 = 0xf0,
  90. JPG1 = 0xf1,
  91. JPG2 = 0xf2,
  92. JPG3 = 0xf3,
  93. JPG4 = 0xf4,
  94. JPG5 = 0xf5,
  95. JPG6 = 0xf6,
  96. SOF48 = 0xf7, ///< JPEG-LS
  97. LSE = 0xf8, ///< JPEG-LS extension parameters
  98. JPG9 = 0xf9,
  99. JPG10 = 0xfa,
  100. JPG11 = 0xfb,
  101. JPG12 = 0xfc,
  102. JPG13 = 0xfd,
  103. COM = 0xfe, /* comment */
  104. TEM = 0x01, /* temporary private use for arithmetic coding */
  105. /* 0x02 -> 0xbf reserved */
  106. } JPEG_MARKER;
  107. static inline void put_marker(PutBitContext *p, int code)
  108. {
  109. put_bits(p, 8, 0xff);
  110. put_bits(p, 8, code);
  111. }
  112. #define MAX_COMPONENTS 4
  113. typedef struct MJpegDecodeContext {
  114. AVCodecContext *avctx;
  115. GetBitContext gb;
  116. int start_code; /* current start code */
  117. int buffer_size;
  118. uint8_t *buffer;
  119. int16_t quant_matrixes[4][64];
  120. VLC vlcs[2][4];
  121. int qscale[4]; ///< quantizer scale calculated from quant_matrixes
  122. int org_height; /* size given at codec init */
  123. int first_picture; /* true if decoding first picture */
  124. int interlaced; /* true if interlaced */
  125. int bottom_field; /* true if bottom field */
  126. int lossless;
  127. int ls;
  128. int progressive;
  129. int rgb;
  130. int rct; /* standard rct */
  131. int pegasus_rct; /* pegasus reversible colorspace transform */
  132. int bits; /* bits per component */
  133. int maxval;
  134. int near; ///< near lossless bound (si 0 for lossless)
  135. int t1,t2,t3;
  136. int reset; ///< context halfing intervall ?rename
  137. int width, height;
  138. int mb_width, mb_height;
  139. int nb_components;
  140. int component_id[MAX_COMPONENTS];
  141. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  142. int v_count[MAX_COMPONENTS];
  143. int comp_index[MAX_COMPONENTS];
  144. int dc_index[MAX_COMPONENTS];
  145. int ac_index[MAX_COMPONENTS];
  146. int nb_blocks[MAX_COMPONENTS];
  147. int h_scount[MAX_COMPONENTS];
  148. int v_scount[MAX_COMPONENTS];
  149. int h_max, v_max; /* maximum h and v counts */
  150. int quant_index[4]; /* quant table index for each component */
  151. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  152. AVFrame picture; /* picture structure */
  153. int linesize[MAX_COMPONENTS]; ///< linesize << interlaced
  154. int8_t *qscale_table;
  155. DECLARE_ALIGNED_8(DCTELEM, block[64]);
  156. ScanTable scantable;
  157. DSPContext dsp;
  158. int restart_interval;
  159. int restart_count;
  160. int buggy_avid;
  161. int cs_itu601;
  162. int interlace_polarity;
  163. int mjpb_skiptosod;
  164. int cur_scan; /* current scan, used by JPEG-LS */
  165. } MJpegDecodeContext;
  166. #define PREDICT(ret, topleft, top, left, predictor)\
  167. switch(predictor){\
  168. case 1: ret= left; break;\
  169. case 2: ret= top; break;\
  170. case 3: ret= topleft; break;\
  171. case 4: ret= left + top - topleft; break;\
  172. case 5: ret= left + ((top - topleft)>>1); break;\
  173. case 6: ret= top + ((left - topleft)>>1); break;\
  174. default:\
  175. case 7: ret= (left + top)>>1; break;\
  176. }
  177. extern const uint8_t ff_mjpeg_bits_dc_luminance[];
  178. extern const uint8_t ff_mjpeg_val_dc_luminance[];
  179. extern const uint8_t ff_mjpeg_bits_dc_chrominance[];
  180. extern const uint8_t ff_mjpeg_val_dc_chrominance[];
  181. extern const uint8_t ff_mjpeg_bits_ac_luminance[];
  182. extern const uint8_t ff_mjpeg_val_ac_luminance[];
  183. extern const uint8_t ff_mjpeg_bits_ac_chrominance[];
  184. extern const uint8_t ff_mjpeg_val_ac_chrominance[];
  185. void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
  186. const uint8_t *bits_table,
  187. const uint8_t *val_table);
  188. #endif /* MJPEG_H */