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.

317 lines
12KB

  1. /*
  2. * Constants for DV codec
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Constants for DV codec.
  24. */
  25. #ifndef AVCODEC_DVDATA_H
  26. #define AVCODEC_DVDATA_H
  27. #include "libavutil/rational.h"
  28. #include "avcodec.h"
  29. typedef struct DVwork_chunk {
  30. uint16_t buf_offset;
  31. uint16_t mb_coordinates[5];
  32. } DVwork_chunk;
  33. /*
  34. * DVprofile is used to express the differences between various
  35. * DV flavors. For now it's primarily used for differentiating
  36. * 525/60 and 625/50, but the plans are to use it for various
  37. * DV specs as well (e.g. SMPTE314M vs. IEC 61834).
  38. */
  39. typedef struct DVprofile {
  40. int dsf; /* value of the dsf in the DV header */
  41. int video_stype; /* stype for VAUX source pack */
  42. int frame_size; /* total size of one frame in bytes */
  43. int difseg_size; /* number of DIF segments per DIF channel */
  44. int n_difchan; /* number of DIF channels per frame */
  45. AVRational time_base; /* 1/framerate */
  46. int ltc_divisor; /* FPS from the LTS standpoint */
  47. int height; /* picture height in pixels */
  48. int width; /* picture width in pixels */
  49. AVRational sar[2]; /* sample aspect ratios for 4:3 and 16:9 */
  50. DVwork_chunk *work_chunks; /* each thread gets its own chunk of frame to work on */
  51. uint32_t *idct_factor; /* set of iDCT factor tables */
  52. enum PixelFormat pix_fmt; /* picture pixel format */
  53. int bpm; /* blocks per macroblock */
  54. const uint8_t *block_sizes; /* AC block sizes, in bits */
  55. int audio_stride; /* size of audio_shuffle table */
  56. int audio_min_samples[3]; /* min amount of audio samples */
  57. /* for 48kHz, 44.1kHz and 32kHz */
  58. int audio_samples_dist[5]; /* how many samples are supposed to be */
  59. /* in each frame in a 5 frames window */
  60. const uint8_t (*audio_shuffle)[9]; /* PCM shuffling table */
  61. } DVprofile;
  62. /* unquant tables (not used directly) */
  63. static const uint8_t dv_quant_shifts[22][4] = {
  64. { 3,3,4,4 },
  65. { 3,3,4,4 },
  66. { 2,3,3,4 },
  67. { 2,3,3,4 },
  68. { 2,2,3,3 },
  69. { 2,2,3,3 },
  70. { 1,2,2,3 },
  71. { 1,2,2,3 },
  72. { 1,1,2,2 },
  73. { 1,1,2,2 },
  74. { 0,1,1,2 },
  75. { 0,1,1,2 },
  76. { 0,0,1,1 },
  77. { 0,0,1,1 },
  78. { 0,0,0,1 },
  79. { 0,0,0,0 },
  80. { 0,0,0,0 },
  81. { 0,0,0,0 },
  82. { 0,0,0,0 },
  83. { 0,0,0,0 },
  84. { 0,0,0,0 },
  85. { 0,0,0,0 },
  86. };
  87. static const uint8_t dv_quant_offset[4] = { 6, 3, 0, 1 };
  88. static const uint8_t dv_quant_areas[4] = { 6, 21, 43, 64 };
  89. /* quantization quanta by QNO for DV100 */
  90. static const uint8_t dv100_qstep[16] = {
  91. 1, /* QNO = 0 and 1 both have no quantization */
  92. 1,
  93. 2, 3, 4, 5, 6, 7, 8, 16, 18, 20, 22, 24, 28, 52
  94. };
  95. /* DV25/50 DCT coefficient weights and inverse weights */
  96. /* created by dvtables.py */
  97. static const int dv_weight_bits = 18;
  98. static const int dv_weight_88[64] = {
  99. 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
  100. 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
  101. 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
  102. 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
  103. 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
  104. 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
  105. 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
  106. 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
  107. };
  108. static const int dv_weight_248[64] = {
  109. 131072, 242189, 257107, 237536, 229376, 200636, 242189, 223754,
  110. 224969, 196781, 262144, 242189, 229376, 200636, 257107, 237536,
  111. 211916, 185364, 235923, 217965, 229376, 211916, 206433, 180568,
  112. 242189, 223754, 224969, 196781, 211916, 185364, 235923, 217965,
  113. 200704, 175557, 222935, 205965, 200636, 185364, 195068, 170627,
  114. 229376, 211916, 206433, 180568, 200704, 175557, 222935, 205965,
  115. 175557, 153560, 188995, 174609, 165371, 144651, 200636, 185364,
  116. 195068, 170627, 175557, 153560, 188995, 174609, 165371, 144651,
  117. };
  118. static const int dv_iweight_bits = 14;
  119. static const int dv_iweight_88[64] = {
  120. 32768, 16710, 16710, 17735, 17015, 17735, 18197, 18079,
  121. 18079, 18197, 18725, 18559, 19196, 18559, 18725, 19284,
  122. 19108, 19692, 19692, 19108, 19284, 21400, 19645, 20262,
  123. 20214, 20262, 19645, 21400, 22733, 21845, 20867, 20815,
  124. 20815, 20867, 21845, 22733, 23173, 23173, 21400, 21400,
  125. 21400, 23173, 23173, 24600, 23764, 22017, 22017, 23764,
  126. 24600, 25267, 24457, 22672, 24457, 25267, 25971, 25191,
  127. 25191, 25971, 26715, 27962, 26715, 29642, 29642, 31536,
  128. };
  129. static const int dv_iweight_248[64] = {
  130. 32768, 17735, 16710, 18079, 18725, 21400, 17735, 19196,
  131. 19108, 21845, 16384, 17735, 18725, 21400, 16710, 18079,
  132. 20262, 23173, 18197, 19692, 18725, 20262, 20815, 23764,
  133. 17735, 19196, 19108, 21845, 20262, 23173, 18197, 19692,
  134. 21400, 24457, 19284, 20867, 21400, 23173, 22017, 25191,
  135. 18725, 20262, 20815, 23764, 21400, 24457, 19284, 20867,
  136. 24457, 27962, 22733, 24600, 25971, 29642, 21400, 23173,
  137. 22017, 25191, 24457, 27962, 22733, 24600, 25971, 29642,
  138. };
  139. /**
  140. * The "inverse" DV100 weights are actually just the spec weights (zig-zagged).
  141. */
  142. static const int dv_iweight_1080_y[64] = {
  143. 128, 16, 16, 17, 17, 17, 18, 18,
  144. 18, 18, 18, 18, 19, 18, 18, 19,
  145. 19, 19, 19, 19, 19, 42, 38, 40,
  146. 40, 40, 38, 42, 44, 43, 41, 41,
  147. 41, 41, 43, 44, 45, 45, 42, 42,
  148. 42, 45, 45, 48, 46, 43, 43, 46,
  149. 48, 49, 48, 44, 48, 49, 101, 98,
  150. 98, 101, 104, 109, 104, 116, 116, 123,
  151. };
  152. static const int dv_iweight_1080_c[64] = {
  153. 128, 16, 16, 17, 17, 17, 25, 25,
  154. 25, 25, 26, 25, 26, 25, 26, 26,
  155. 26, 27, 27, 26, 26, 42, 38, 40,
  156. 40, 40, 38, 42, 44, 43, 41, 41,
  157. 41, 41, 43, 44, 91, 91, 84, 84,
  158. 84, 91, 91, 96, 93, 86, 86, 93,
  159. 96, 197, 191, 177, 191, 197, 203, 197,
  160. 197, 203, 209, 219, 209, 232, 232, 246,
  161. };
  162. static const int dv_iweight_720_y[64] = {
  163. 128, 16, 16, 17, 17, 17, 18, 18,
  164. 18, 18, 18, 18, 19, 18, 18, 19,
  165. 19, 19, 19, 19, 19, 42, 38, 40,
  166. 40, 40, 38, 42, 44, 43, 41, 41,
  167. 41, 41, 43, 44, 68, 68, 63, 63,
  168. 63, 68, 68, 96, 92, 86, 86, 92,
  169. 96, 98, 96, 88, 96, 98, 202, 196,
  170. 196, 202, 208, 218, 208, 232, 232, 246,
  171. };
  172. static const int dv_iweight_720_c[64] = {
  173. 128, 24, 24, 26, 26, 26, 36, 36,
  174. 36, 36, 36, 36, 38, 36, 36, 38,
  175. 38, 38, 38, 38, 38, 84, 76, 80,
  176. 80, 80, 76, 84, 88, 86, 82, 82,
  177. 82, 82, 86, 88, 182, 182, 168, 168,
  178. 168, 182, 182, 192, 186, 192, 172, 186,
  179. 192, 394, 382, 354, 382, 394, 406, 394,
  180. 394, 406, 418, 438, 418, 464, 464, 492,
  181. };
  182. static const uint8_t dv_audio_shuffle525[10][9] = {
  183. { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
  184. { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
  185. { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
  186. { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
  187. { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
  188. { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
  189. { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
  190. { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
  191. { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
  192. { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
  193. };
  194. static const uint8_t dv_audio_shuffle625[12][9] = {
  195. { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */
  196. { 6, 42, 78, 32, 68, 104, 22, 58, 94},
  197. { 12, 48, 84, 2, 38, 74, 28, 64, 100},
  198. { 18, 54, 90, 8, 44, 80, 34, 70, 106},
  199. { 24, 60, 96, 14, 50, 86, 4, 40, 76},
  200. { 30, 66, 102, 20, 56, 92, 10, 46, 82},
  201. { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */
  202. { 7, 43, 79, 33, 69, 105, 23, 59, 95},
  203. { 13, 49, 85, 3, 39, 75, 29, 65, 101},
  204. { 19, 55, 91, 9, 45, 81, 35, 71, 107},
  205. { 25, 61, 97, 15, 51, 87, 5, 41, 77},
  206. { 31, 67, 103, 21, 57, 93, 11, 47, 83},
  207. };
  208. static const av_unused int dv_audio_frequency[3] = {
  209. 48000, 44100, 32000,
  210. };
  211. /* macroblock bit budgets */
  212. static const uint8_t block_sizes_dv2550[8] = {
  213. 112, 112, 112, 112, 80, 80, 0, 0,
  214. };
  215. static const uint8_t block_sizes_dv100[8] = {
  216. 80, 80, 80, 80, 80, 80, 64, 64,
  217. };
  218. enum dv_section_type {
  219. dv_sect_header = 0x1f,
  220. dv_sect_subcode = 0x3f,
  221. dv_sect_vaux = 0x56,
  222. dv_sect_audio = 0x76,
  223. dv_sect_video = 0x96,
  224. };
  225. enum dv_pack_type {
  226. dv_header525 = 0x3f, /* see dv_write_pack for important details on */
  227. dv_header625 = 0xbf, /* these two packs */
  228. dv_timecode = 0x13,
  229. dv_audio_source = 0x50,
  230. dv_audio_control = 0x51,
  231. dv_audio_recdate = 0x52,
  232. dv_audio_rectime = 0x53,
  233. dv_video_source = 0x60,
  234. dv_video_control = 0x61,
  235. dv_video_recdate = 0x62,
  236. dv_video_rectime = 0x63,
  237. dv_unknown_pack = 0xff,
  238. };
  239. #define DV_PROFILE_IS_HD(p) ((p)->video_stype & 0x10)
  240. #define DV_PROFILE_IS_1080i50(p) (((p)->video_stype == 0x14) && ((p)->dsf == 1))
  241. #define DV_PROFILE_IS_720p50(p) (((p)->video_stype == 0x18) && ((p)->dsf == 1))
  242. /* minimum number of bytes to read from a DV stream in order to
  243. determine the profile */
  244. #define DV_PROFILE_BYTES (6*80) /* 6 DIF blocks */
  245. /**
  246. * largest possible DV frame, in bytes (1080i50)
  247. */
  248. #define DV_MAX_FRAME_SIZE 576000
  249. /**
  250. * maximum number of blocks per macroblock in any DV format
  251. */
  252. #define DV_MAX_BPM 8
  253. const DVprofile* ff_dv_frame_profile(const DVprofile *sys,
  254. const uint8_t* frame, unsigned buf_size);
  255. const DVprofile* ff_dv_codec_profile(AVCodecContext* codec);
  256. static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
  257. uint8_t seq_num, uint8_t dif_num,
  258. uint8_t* buf)
  259. {
  260. buf[0] = (uint8_t)t; /* Section type */
  261. buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
  262. (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
  263. 7; /* reserved -- always 1 */
  264. buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
  265. return 3;
  266. }
  267. static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
  268. {
  269. if (syb_num == 0 || syb_num == 6) {
  270. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  271. (0 << 4) | /* AP3 (Subcode application ID) */
  272. 0x0f; /* reserved -- always 1 */
  273. }
  274. else if (syb_num == 11) {
  275. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  276. 0x7f; /* reserved -- always 1 */
  277. }
  278. else {
  279. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  280. (0 << 4) | /* APT (Track application ID) */
  281. 0x0f; /* reserved -- always 1 */
  282. }
  283. buf[1] = 0xf0 | /* reserved -- always 1 */
  284. (syb_num & 0x0f); /* SSYB number 0 - 11 */
  285. buf[2] = 0xff; /* reserved -- always 1 */
  286. return 3;
  287. }
  288. #endif /* AVCODEC_DVDATA_H */