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.

756 lines
28KB

  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 dvdata.h
  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. enum PixelFormat pix_fmt; /* picture pixel format */
  52. int bpm; /* blocks per macroblock */
  53. const uint8_t *block_sizes; /* AC block sizes, in bits */
  54. int audio_stride; /* size of audio_shuffle table */
  55. int audio_min_samples[3]; /* min amount of audio samples */
  56. /* for 48kHz, 44.1kHz and 32kHz */
  57. int audio_samples_dist[5]; /* how many samples are supposed to be */
  58. /* in each frame in a 5 frames window */
  59. const uint8_t (*audio_shuffle)[9]; /* PCM shuffling table */
  60. } DVprofile;
  61. #define NB_DV_VLC 409
  62. /*
  63. * There's a catch about the following three tables: the mapping they establish
  64. * between (run, level) and vlc is not 1-1. So you have to watch out for that
  65. * when building misc. tables. E.g. (1, 0) can be either 0x7cf or 0x1f82.
  66. */
  67. static const uint16_t dv_vlc_bits[409] = {
  68. 0x0000, 0x0002, 0x0007, 0x0008, 0x0009, 0x0014, 0x0015, 0x0016,
  69. 0x0017, 0x0030, 0x0031, 0x0032, 0x0033, 0x0068, 0x0069, 0x006a,
  70. 0x006b, 0x006c, 0x006d, 0x006e, 0x006f, 0x00e0, 0x00e1, 0x00e2,
  71. 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9, 0x00ea,
  72. 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 0x01e0, 0x01e1, 0x01e2,
  73. 0x01e3, 0x01e4, 0x01e5, 0x01e6, 0x01e7, 0x01e8, 0x01e9, 0x01ea,
  74. 0x01eb, 0x01ec, 0x01ed, 0x01ee, 0x01ef, 0x03e0, 0x03e1, 0x03e2,
  75. 0x03e3, 0x03e4, 0x03e5, 0x03e6, 0x07ce, 0x07cf, 0x07d0, 0x07d1,
  76. 0x07d2, 0x07d3, 0x07d4, 0x07d5, 0x0fac, 0x0fad, 0x0fae, 0x0faf,
  77. 0x0fb0, 0x0fb1, 0x0fb2, 0x0fb3, 0x0fb4, 0x0fb5, 0x0fb6, 0x0fb7,
  78. 0x0fb8, 0x0fb9, 0x0fba, 0x0fbb, 0x0fbc, 0x0fbd, 0x0fbe, 0x0fbf,
  79. 0x1f80, 0x1f81, 0x1f82, 0x1f83, 0x1f84, 0x1f85, 0x1f86, 0x1f87,
  80. 0x1f88, 0x1f89, 0x1f8a, 0x1f8b, 0x1f8c, 0x1f8d, 0x1f8e, 0x1f8f,
  81. 0x1f90, 0x1f91, 0x1f92, 0x1f93, 0x1f94, 0x1f95, 0x1f96, 0x1f97,
  82. 0x1f98, 0x1f99, 0x1f9a, 0x1f9b, 0x1f9c, 0x1f9d, 0x1f9e, 0x1f9f,
  83. 0x1fa0, 0x1fa1, 0x1fa2, 0x1fa3, 0x1fa4, 0x1fa5, 0x1fa6, 0x1fa7,
  84. 0x1fa8, 0x1fa9, 0x1faa, 0x1fab, 0x1fac, 0x1fad, 0x1fae, 0x1faf,
  85. 0x1fb0, 0x1fb1, 0x1fb2, 0x1fb3, 0x1fb4, 0x1fb5, 0x1fb6, 0x1fb7,
  86. 0x1fb8, 0x1fb9, 0x1fba, 0x1fbb, 0x1fbc, 0x1fbd, 0x1fbe, 0x1fbf,
  87. 0x7f00, 0x7f01, 0x7f02, 0x7f03, 0x7f04, 0x7f05, 0x7f06, 0x7f07,
  88. 0x7f08, 0x7f09, 0x7f0a, 0x7f0b, 0x7f0c, 0x7f0d, 0x7f0e, 0x7f0f,
  89. 0x7f10, 0x7f11, 0x7f12, 0x7f13, 0x7f14, 0x7f15, 0x7f16, 0x7f17,
  90. 0x7f18, 0x7f19, 0x7f1a, 0x7f1b, 0x7f1c, 0x7f1d, 0x7f1e, 0x7f1f,
  91. 0x7f20, 0x7f21, 0x7f22, 0x7f23, 0x7f24, 0x7f25, 0x7f26, 0x7f27,
  92. 0x7f28, 0x7f29, 0x7f2a, 0x7f2b, 0x7f2c, 0x7f2d, 0x7f2e, 0x7f2f,
  93. 0x7f30, 0x7f31, 0x7f32, 0x7f33, 0x7f34, 0x7f35, 0x7f36, 0x7f37,
  94. 0x7f38, 0x7f39, 0x7f3a, 0x7f3b, 0x7f3c, 0x7f3d, 0x7f3e, 0x7f3f,
  95. 0x7f40, 0x7f41, 0x7f42, 0x7f43, 0x7f44, 0x7f45, 0x7f46, 0x7f47,
  96. 0x7f48, 0x7f49, 0x7f4a, 0x7f4b, 0x7f4c, 0x7f4d, 0x7f4e, 0x7f4f,
  97. 0x7f50, 0x7f51, 0x7f52, 0x7f53, 0x7f54, 0x7f55, 0x7f56, 0x7f57,
  98. 0x7f58, 0x7f59, 0x7f5a, 0x7f5b, 0x7f5c, 0x7f5d, 0x7f5e, 0x7f5f,
  99. 0x7f60, 0x7f61, 0x7f62, 0x7f63, 0x7f64, 0x7f65, 0x7f66, 0x7f67,
  100. 0x7f68, 0x7f69, 0x7f6a, 0x7f6b, 0x7f6c, 0x7f6d, 0x7f6e, 0x7f6f,
  101. 0x7f70, 0x7f71, 0x7f72, 0x7f73, 0x7f74, 0x7f75, 0x7f76, 0x7f77,
  102. 0x7f78, 0x7f79, 0x7f7a, 0x7f7b, 0x7f7c, 0x7f7d, 0x7f7e, 0x7f7f,
  103. 0x7f80, 0x7f81, 0x7f82, 0x7f83, 0x7f84, 0x7f85, 0x7f86, 0x7f87,
  104. 0x7f88, 0x7f89, 0x7f8a, 0x7f8b, 0x7f8c, 0x7f8d, 0x7f8e, 0x7f8f,
  105. 0x7f90, 0x7f91, 0x7f92, 0x7f93, 0x7f94, 0x7f95, 0x7f96, 0x7f97,
  106. 0x7f98, 0x7f99, 0x7f9a, 0x7f9b, 0x7f9c, 0x7f9d, 0x7f9e, 0x7f9f,
  107. 0x7fa0, 0x7fa1, 0x7fa2, 0x7fa3, 0x7fa4, 0x7fa5, 0x7fa6, 0x7fa7,
  108. 0x7fa8, 0x7fa9, 0x7faa, 0x7fab, 0x7fac, 0x7fad, 0x7fae, 0x7faf,
  109. 0x7fb0, 0x7fb1, 0x7fb2, 0x7fb3, 0x7fb4, 0x7fb5, 0x7fb6, 0x7fb7,
  110. 0x7fb8, 0x7fb9, 0x7fba, 0x7fbb, 0x7fbc, 0x7fbd, 0x7fbe, 0x7fbf,
  111. 0x7fc0, 0x7fc1, 0x7fc2, 0x7fc3, 0x7fc4, 0x7fc5, 0x7fc6, 0x7fc7,
  112. 0x7fc8, 0x7fc9, 0x7fca, 0x7fcb, 0x7fcc, 0x7fcd, 0x7fce, 0x7fcf,
  113. 0x7fd0, 0x7fd1, 0x7fd2, 0x7fd3, 0x7fd4, 0x7fd5, 0x7fd6, 0x7fd7,
  114. 0x7fd8, 0x7fd9, 0x7fda, 0x7fdb, 0x7fdc, 0x7fdd, 0x7fde, 0x7fdf,
  115. 0x7fe0, 0x7fe1, 0x7fe2, 0x7fe3, 0x7fe4, 0x7fe5, 0x7fe6, 0x7fe7,
  116. 0x7fe8, 0x7fe9, 0x7fea, 0x7feb, 0x7fec, 0x7fed, 0x7fee, 0x7fef,
  117. 0x7ff0, 0x7ff1, 0x7ff2, 0x7ff3, 0x7ff4, 0x7ff5, 0x7ff6, 0x7ff7,
  118. 0x7ff8, 0x7ff9, 0x7ffa, 0x7ffb, 0x7ffc, 0x7ffd, 0x7ffe, 0x7fff,
  119. 0x0006,
  120. };
  121. static const uint8_t dv_vlc_len[409] = {
  122. 2, 3, 4, 4, 4, 5, 5, 5,
  123. 5, 6, 6, 6, 6, 7, 7, 7,
  124. 7, 7, 7, 7, 7, 8, 8, 8,
  125. 8, 8, 8, 8, 8, 8, 8, 8,
  126. 8, 8, 8, 8, 8, 9, 9, 9,
  127. 9, 9, 9, 9, 9, 9, 9, 9,
  128. 9, 9, 9, 9, 9, 10, 10, 10,
  129. 10, 10, 10, 10, 11, 11, 11, 11,
  130. 11, 11, 11, 11, 12, 12, 12, 12,
  131. 12, 12, 12, 12, 12, 12, 12, 12,
  132. 12, 12, 12, 12, 12, 12, 12, 12,
  133. 13, 13, 13, 13, 13, 13, 13, 13,
  134. 13, 13, 13, 13, 13, 13, 13, 13,
  135. 13, 13, 13, 13, 13, 13, 13, 13,
  136. 13, 13, 13, 13, 13, 13, 13, 13,
  137. 13, 13, 13, 13, 13, 13, 13, 13,
  138. 13, 13, 13, 13, 13, 13, 13, 13,
  139. 13, 13, 13, 13, 13, 13, 13, 13,
  140. 13, 13, 13, 13, 13, 13, 13, 13,
  141. 15, 15, 15, 15, 15, 15, 15, 15,
  142. 15, 15, 15, 15, 15, 15, 15, 15,
  143. 15, 15, 15, 15, 15, 15, 15, 15,
  144. 15, 15, 15, 15, 15, 15, 15, 15,
  145. 15, 15, 15, 15, 15, 15, 15, 15,
  146. 15, 15, 15, 15, 15, 15, 15, 15,
  147. 15, 15, 15, 15, 15, 15, 15, 15,
  148. 15, 15, 15, 15, 15, 15, 15, 15,
  149. 15, 15, 15, 15, 15, 15, 15, 15,
  150. 15, 15, 15, 15, 15, 15, 15, 15,
  151. 15, 15, 15, 15, 15, 15, 15, 15,
  152. 15, 15, 15, 15, 15, 15, 15, 15,
  153. 15, 15, 15, 15, 15, 15, 15, 15,
  154. 15, 15, 15, 15, 15, 15, 15, 15,
  155. 15, 15, 15, 15, 15, 15, 15, 15,
  156. 15, 15, 15, 15, 15, 15, 15, 15,
  157. 15, 15, 15, 15, 15, 15, 15, 15,
  158. 15, 15, 15, 15, 15, 15, 15, 15,
  159. 15, 15, 15, 15, 15, 15, 15, 15,
  160. 15, 15, 15, 15, 15, 15, 15, 15,
  161. 15, 15, 15, 15, 15, 15, 15, 15,
  162. 15, 15, 15, 15, 15, 15, 15, 15,
  163. 15, 15, 15, 15, 15, 15, 15, 15,
  164. 15, 15, 15, 15, 15, 15, 15, 15,
  165. 15, 15, 15, 15, 15, 15, 15, 15,
  166. 15, 15, 15, 15, 15, 15, 15, 15,
  167. 15, 15, 15, 15, 15, 15, 15, 15,
  168. 15, 15, 15, 15, 15, 15, 15, 15,
  169. 15, 15, 15, 15, 15, 15, 15, 15,
  170. 15, 15, 15, 15, 15, 15, 15, 15,
  171. 15, 15, 15, 15, 15, 15, 15, 15,
  172. 15, 15, 15, 15, 15, 15, 15, 15,
  173. 4,
  174. };
  175. static const uint8_t dv_vlc_run[409] = {
  176. 0, 0, 1, 0, 0, 2, 1, 0,
  177. 0, 3, 4, 0, 0, 5, 6, 2,
  178. 1, 1, 0, 0, 0, 7, 8, 9,
  179. 10, 3, 4, 2, 1, 1, 1, 0,
  180. 0, 0, 0, 0, 0, 11, 12, 13,
  181. 14, 5, 6, 3, 4, 2, 2, 1,
  182. 0, 0, 0, 0, 0, 5, 3, 3,
  183. 2, 1, 1, 1, 0, 1, 6, 4,
  184. 3, 1, 1, 1, 2, 3, 4, 5,
  185. 7, 8, 9, 10, 7, 8, 4, 3,
  186. 2, 2, 2, 2, 2, 1, 1, 1,
  187. 0, 1, 2, 3, 4, 5, 6, 7,
  188. 8, 9, 10, 11, 12, 13, 14, 15,
  189. 16, 17, 18, 19, 20, 21, 22, 23,
  190. 24, 25, 26, 27, 28, 29, 30, 31,
  191. 32, 33, 34, 35, 36, 37, 38, 39,
  192. 40, 41, 42, 43, 44, 45, 46, 47,
  193. 48, 49, 50, 51, 52, 53, 54, 55,
  194. 56, 57, 58, 59, 60, 61, 62, 63,
  195. 0, 0, 0, 0, 0, 0, 0, 0,
  196. 0, 0, 0, 0, 0, 0, 0, 0,
  197. 0, 0, 0, 0, 0, 0, 0, 0,
  198. 0, 0, 0, 0, 0, 0, 0, 0,
  199. 0, 0, 0, 0, 0, 0, 0, 0,
  200. 0, 0, 0, 0, 0, 0, 0, 0,
  201. 0, 0, 0, 0, 0, 0, 0, 0,
  202. 0, 0, 0, 0, 0, 0, 0, 0,
  203. 0, 0, 0, 0, 0, 0, 0, 0,
  204. 0, 0, 0, 0, 0, 0, 0, 0,
  205. 0, 0, 0, 0, 0, 0, 0, 0,
  206. 0, 0, 0, 0, 0, 0, 0, 0,
  207. 0, 0, 0, 0, 0, 0, 0, 0,
  208. 0, 0, 0, 0, 0, 0, 0, 0,
  209. 0, 0, 0, 0, 0, 0, 0, 0,
  210. 0, 0, 0, 0, 0, 0, 0, 0,
  211. 0, 0, 0, 0, 0, 0, 0, 0,
  212. 0, 0, 0, 0, 0, 0, 0, 0,
  213. 0, 0, 0, 0, 0, 0, 0, 0,
  214. 0, 0, 0, 0, 0, 0, 0, 0,
  215. 0, 0, 0, 0, 0, 0, 0, 0,
  216. 0, 0, 0, 0, 0, 0, 0, 0,
  217. 0, 0, 0, 0, 0, 0, 0, 0,
  218. 0, 0, 0, 0, 0, 0, 0, 0,
  219. 0, 0, 0, 0, 0, 0, 0, 0,
  220. 0, 0, 0, 0, 0, 0, 0, 0,
  221. 0, 0, 0, 0, 0, 0, 0, 0,
  222. 0, 0, 0, 0, 0, 0, 0, 0,
  223. 0, 0, 0, 0, 0, 0, 0, 0,
  224. 0, 0, 0, 0, 0, 0, 0, 0,
  225. 0, 0, 0, 0, 0, 0, 0, 0,
  226. 0, 0, 0, 0, 0, 0, 0, 0,
  227. 127,
  228. };
  229. static const uint8_t dv_vlc_level[409] = {
  230. 1, 2, 1, 3, 4, 1, 2, 5,
  231. 6, 1, 1, 7, 8, 1, 1, 2,
  232. 3, 4, 9, 10, 11, 1, 1, 1,
  233. 1, 2, 2, 3, 5, 6, 7, 12,
  234. 13, 14, 15, 16, 17, 1, 1, 1,
  235. 1, 2, 2, 3, 3, 4, 5, 8,
  236. 18, 19, 20, 21, 22, 3, 4, 5,
  237. 6, 9, 10, 11, 0, 0, 3, 4,
  238. 6, 12, 13, 14, 0, 0, 0, 0,
  239. 2, 2, 2, 2, 3, 3, 5, 7,
  240. 7, 8, 9, 10, 11, 15, 16, 17,
  241. 0, 0, 0, 0, 0, 0, 0, 0,
  242. 0, 0, 0, 0, 0, 0, 0, 0,
  243. 0, 0, 0, 0, 0, 0, 0, 0,
  244. 0, 0, 0, 0, 0, 0, 0, 0,
  245. 0, 0, 0, 0, 0, 0, 0, 0,
  246. 0, 0, 0, 0, 0, 0, 0, 0,
  247. 0, 0, 0, 0, 0, 0, 0, 0,
  248. 0, 0, 0, 0, 0, 0, 0, 0,
  249. 0, 1, 2, 3, 4, 5, 6, 7,
  250. 8, 9, 10, 11, 12, 13, 14, 15,
  251. 16, 17, 18, 19, 20, 21, 22, 23,
  252. 24, 25, 26, 27, 28, 29, 30, 31,
  253. 32, 33, 34, 35, 36, 37, 38, 39,
  254. 40, 41, 42, 43, 44, 45, 46, 47,
  255. 48, 49, 50, 51, 52, 53, 54, 55,
  256. 56, 57, 58, 59, 60, 61, 62, 63,
  257. 64, 65, 66, 67, 68, 69, 70, 71,
  258. 72, 73, 74, 75, 76, 77, 78, 79,
  259. 80, 81, 82, 83, 84, 85, 86, 87,
  260. 88, 89, 90, 91, 92, 93, 94, 95,
  261. 96, 97, 98, 99, 100, 101, 102, 103,
  262. 104, 105, 106, 107, 108, 109, 110, 111,
  263. 112, 113, 114, 115, 116, 117, 118, 119,
  264. 120, 121, 122, 123, 124, 125, 126, 127,
  265. 128, 129, 130, 131, 132, 133, 134, 135,
  266. 136, 137, 138, 139, 140, 141, 142, 143,
  267. 144, 145, 146, 147, 148, 149, 150, 151,
  268. 152, 153, 154, 155, 156, 157, 158, 159,
  269. 160, 161, 162, 163, 164, 165, 166, 167,
  270. 168, 169, 170, 171, 172, 173, 174, 175,
  271. 176, 177, 178, 179, 180, 181, 182, 183,
  272. 184, 185, 186, 187, 188, 189, 190, 191,
  273. 192, 193, 194, 195, 196, 197, 198, 199,
  274. 200, 201, 202, 203, 204, 205, 206, 207,
  275. 208, 209, 210, 211, 212, 213, 214, 215,
  276. 216, 217, 218, 219, 220, 221, 222, 223,
  277. 224, 225, 226, 227, 228, 229, 230, 231,
  278. 232, 233, 234, 235, 236, 237, 238, 239,
  279. 240, 241, 242, 243, 244, 245, 246, 247,
  280. 248, 249, 250, 251, 252, 253, 254, 255,
  281. 0,
  282. };
  283. /* unquant tables (not used directly) */
  284. static const uint8_t dv_quant_shifts[22][4] = {
  285. { 3,3,4,4 },
  286. { 3,3,4,4 },
  287. { 2,3,3,4 },
  288. { 2,3,3,4 },
  289. { 2,2,3,3 },
  290. { 2,2,3,3 },
  291. { 1,2,2,3 },
  292. { 1,2,2,3 },
  293. { 1,1,2,2 },
  294. { 1,1,2,2 },
  295. { 0,1,1,2 },
  296. { 0,1,1,2 },
  297. { 0,0,1,1 },
  298. { 0,0,1,1 },
  299. { 0,0,0,1 },
  300. { 0,0,0,0 },
  301. { 0,0,0,0 },
  302. { 0,0,0,0 },
  303. { 0,0,0,0 },
  304. { 0,0,0,0 },
  305. { 0,0,0,0 },
  306. { 0,0,0,0 },
  307. };
  308. static const uint8_t dv_quant_offset[4] = { 6, 3, 0, 1 };
  309. static const uint8_t dv_quant_areas[4] = { 6, 21, 43, 64 };
  310. /* quantization quanta by QNO for DV100 */
  311. static const uint8_t dv100_qstep[16] = {
  312. 1, /* QNO = 0 and 1 both have no quantization */
  313. 1,
  314. 2, 3, 4, 5, 6, 7, 8, 16, 18, 20, 22, 24, 28, 52
  315. };
  316. /* DV25/50 DCT coefficient weights and inverse weights */
  317. /* created by dvtables.py */
  318. static const int dv_weight_bits = 18;
  319. static const int dv_weight_88[64] = {
  320. 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
  321. 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
  322. 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
  323. 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
  324. 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
  325. 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
  326. 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
  327. 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
  328. };
  329. static const int dv_weight_248[64] = {
  330. 131072, 242189, 257107, 237536, 229376, 200636, 242189, 223754,
  331. 224969, 196781, 262144, 242189, 229376, 200636, 257107, 237536,
  332. 211916, 185364, 235923, 217965, 229376, 211916, 206433, 180568,
  333. 242189, 223754, 224969, 196781, 211916, 185364, 235923, 217965,
  334. 200704, 175557, 222935, 205965, 200636, 185364, 195068, 170627,
  335. 229376, 211916, 206433, 180568, 200704, 175557, 222935, 205965,
  336. 175557, 153560, 188995, 174609, 165371, 144651, 200636, 185364,
  337. 195068, 170627, 175557, 153560, 188995, 174609, 165371, 144651,
  338. };
  339. static const int dv_iweight_bits = 14;
  340. static const int dv_iweight_88[64] = {
  341. 32768, 16710, 16710, 17735, 17015, 17735, 18197, 18079,
  342. 18079, 18197, 18725, 18559, 19196, 18559, 18725, 19284,
  343. 19108, 19692, 19692, 19108, 19284, 21400, 19645, 20262,
  344. 20214, 20262, 19645, 21400, 22733, 21845, 20867, 20815,
  345. 20815, 20867, 21845, 22733, 23173, 23173, 21400, 21400,
  346. 21400, 23173, 23173, 24600, 23764, 22017, 22017, 23764,
  347. 24600, 25267, 24457, 22672, 24457, 25267, 25971, 25191,
  348. 25191, 25971, 26715, 27962, 26715, 29642, 29642, 31536,
  349. };
  350. static const int dv_iweight_248[64] = {
  351. 32768, 17735, 16710, 18079, 18725, 21400, 17735, 19196,
  352. 19108, 21845, 16384, 17735, 18725, 21400, 16710, 18079,
  353. 20262, 23173, 18197, 19692, 18725, 20262, 20815, 23764,
  354. 17735, 19196, 19108, 21845, 20262, 23173, 18197, 19692,
  355. 21400, 24457, 19284, 20867, 21400, 23173, 22017, 25191,
  356. 18725, 20262, 20815, 23764, 21400, 24457, 19284, 20867,
  357. 24457, 27962, 22733, 24600, 25971, 29642, 21400, 23173,
  358. 22017, 25191, 24457, 27962, 22733, 24600, 25971, 29642,
  359. };
  360. /**
  361. * The "inverse" DV100 weights are actually just the spec weights (zig-zagged).
  362. */
  363. static const int dv_iweight_1080_y[64] = {
  364. 128, 16, 16, 17, 17, 17, 18, 18,
  365. 18, 18, 18, 18, 19, 18, 18, 19,
  366. 19, 19, 19, 19, 19, 42, 38, 40,
  367. 40, 40, 38, 42, 44, 43, 41, 41,
  368. 41, 41, 43, 44, 45, 45, 42, 42,
  369. 42, 45, 45, 48, 46, 43, 43, 46,
  370. 48, 49, 48, 44, 48, 49, 101, 98,
  371. 98, 101, 104, 109, 104, 116, 116, 123,
  372. };
  373. static const int dv_iweight_1080_c[64] = {
  374. 128, 16, 16, 17, 17, 17, 25, 25,
  375. 25, 25, 26, 25, 26, 25, 26, 26,
  376. 26, 27, 27, 26, 26, 42, 38, 40,
  377. 40, 40, 38, 42, 44, 43, 41, 41,
  378. 41, 41, 43, 44, 91, 91, 84, 84,
  379. 84, 91, 91, 96, 93, 86, 86, 93,
  380. 96, 197, 191, 177, 191, 197, 203, 197,
  381. 197, 203, 209, 219, 209, 232, 232, 246,
  382. };
  383. static const int dv_iweight_720_y[64] = {
  384. 128, 16, 16, 17, 17, 17, 18, 18,
  385. 18, 18, 18, 18, 19, 18, 18, 19,
  386. 19, 19, 19, 19, 19, 42, 38, 40,
  387. 40, 40, 38, 42, 44, 43, 41, 41,
  388. 41, 41, 43, 44, 68, 68, 63, 63,
  389. 63, 68, 68, 96, 92, 86, 86, 92,
  390. 96, 98, 96, 88, 96, 98, 202, 196,
  391. 196, 202, 208, 218, 208, 232, 232, 246,
  392. };
  393. static const int dv_iweight_720_c[64] = {
  394. 128, 24, 24, 26, 26, 26, 36, 36,
  395. 36, 36, 36, 36, 38, 36, 36, 38,
  396. 38, 38, 38, 38, 38, 84, 76, 80,
  397. 80, 80, 76, 84, 88, 86, 82, 82,
  398. 82, 82, 86, 88, 182, 182, 168, 168,
  399. 168, 182, 182, 192, 186, 192, 172, 186,
  400. 192, 394, 382, 354, 382, 394, 406, 394,
  401. 394, 406, 418, 438, 418, 464, 464, 492,
  402. };
  403. static const uint8_t dv_audio_shuffle525[10][9] = {
  404. { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
  405. { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
  406. { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
  407. { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
  408. { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
  409. { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
  410. { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
  411. { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
  412. { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
  413. { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
  414. };
  415. static const uint8_t dv_audio_shuffle625[12][9] = {
  416. { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */
  417. { 6, 42, 78, 32, 68, 104, 22, 58, 94},
  418. { 12, 48, 84, 2, 38, 74, 28, 64, 100},
  419. { 18, 54, 90, 8, 44, 80, 34, 70, 106},
  420. { 24, 60, 96, 14, 50, 86, 4, 40, 76},
  421. { 30, 66, 102, 20, 56, 92, 10, 46, 82},
  422. { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */
  423. { 7, 43, 79, 33, 69, 105, 23, 59, 95},
  424. { 13, 49, 85, 3, 39, 75, 29, 65, 101},
  425. { 19, 55, 91, 9, 45, 81, 35, 71, 107},
  426. { 25, 61, 97, 15, 51, 87, 5, 41, 77},
  427. { 31, 67, 103, 21, 57, 93, 11, 47, 83},
  428. };
  429. static const av_unused int dv_audio_frequency[3] = {
  430. 48000, 44100, 32000,
  431. };
  432. /* macroblock bit budgets */
  433. static const uint8_t block_sizes_dv2550[8] = {
  434. 112, 112, 112, 112, 80, 80, 0, 0,
  435. };
  436. static const uint8_t block_sizes_dv100[8] = {
  437. 80, 80, 80, 80, 80, 80, 64, 64,
  438. };
  439. static DVwork_chunk work_chunks_dv25pal [1*12*27];
  440. static DVwork_chunk work_chunks_dv25pal411[1*12*27];
  441. static DVwork_chunk work_chunks_dv25ntsc [1*10*27];
  442. static DVwork_chunk work_chunks_dv50pal [2*12*27];
  443. static DVwork_chunk work_chunks_dv50ntsc [2*10*27];
  444. static DVwork_chunk work_chunks_dv100palp [2*12*27];
  445. static DVwork_chunk work_chunks_dv100ntscp[2*10*27];
  446. static DVwork_chunk work_chunks_dv100pali [4*12*27];
  447. static DVwork_chunk work_chunks_dv100ntsci[4*10*27];
  448. static const DVprofile dv_profiles[] = {
  449. { .dsf = 0,
  450. .video_stype = 0x0,
  451. .frame_size = 120000, /* IEC 61834, SMPTE-314M - 525/60 (NTSC) */
  452. .difseg_size = 10,
  453. .n_difchan = 1,
  454. .time_base = { 1001, 30000 },
  455. .ltc_divisor = 30,
  456. .height = 480,
  457. .width = 720,
  458. .sar = {{10, 11}, {40, 33}},
  459. .work_chunks = &work_chunks_dv25ntsc[0],
  460. .pix_fmt = PIX_FMT_YUV411P,
  461. .bpm = 6,
  462. .block_sizes = block_sizes_dv2550,
  463. .audio_stride = 90,
  464. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  465. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  466. .audio_shuffle = dv_audio_shuffle525,
  467. },
  468. { .dsf = 1,
  469. .video_stype = 0x0,
  470. .frame_size = 144000, /* IEC 61834 - 625/50 (PAL) */
  471. .difseg_size = 12,
  472. .n_difchan = 1,
  473. .time_base = { 1, 25 },
  474. .ltc_divisor = 25,
  475. .height = 576,
  476. .width = 720,
  477. .sar = {{59, 54}, {118, 81}},
  478. .work_chunks = &work_chunks_dv25pal[0],
  479. .pix_fmt = PIX_FMT_YUV420P,
  480. .bpm = 6,
  481. .block_sizes = block_sizes_dv2550,
  482. .audio_stride = 108,
  483. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  484. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  485. .audio_shuffle = dv_audio_shuffle625,
  486. },
  487. { .dsf = 1,
  488. .video_stype = 0x0,
  489. .frame_size = 144000, /* SMPTE-314M - 625/50 (PAL) */
  490. .difseg_size = 12,
  491. .n_difchan = 1,
  492. .time_base = { 1, 25 },
  493. .ltc_divisor = 25,
  494. .height = 576,
  495. .width = 720,
  496. .sar = {{59, 54}, {118, 81}},
  497. .work_chunks = &work_chunks_dv25pal411[0],
  498. .pix_fmt = PIX_FMT_YUV411P,
  499. .bpm = 6,
  500. .block_sizes = block_sizes_dv2550,
  501. .audio_stride = 108,
  502. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  503. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  504. .audio_shuffle = dv_audio_shuffle625,
  505. },
  506. { .dsf = 0,
  507. .video_stype = 0x4,
  508. .frame_size = 240000, /* SMPTE-314M - 525/60 (NTSC) 50 Mbps */
  509. .difseg_size = 10, /* also known as "DVCPRO50" */
  510. .n_difchan = 2,
  511. .time_base = { 1001, 30000 },
  512. .ltc_divisor = 30,
  513. .height = 480,
  514. .width = 720,
  515. .sar = {{10, 11}, {40, 33}},
  516. .work_chunks = &work_chunks_dv50ntsc[0],
  517. .pix_fmt = PIX_FMT_YUV422P,
  518. .bpm = 6,
  519. .block_sizes = block_sizes_dv2550,
  520. .audio_stride = 90,
  521. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  522. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  523. .audio_shuffle = dv_audio_shuffle525,
  524. },
  525. { .dsf = 1,
  526. .video_stype = 0x4,
  527. .frame_size = 288000, /* SMPTE-314M - 625/50 (PAL) 50 Mbps */
  528. .difseg_size = 12, /* also known as "DVCPRO50" */
  529. .n_difchan = 2,
  530. .time_base = { 1, 25 },
  531. .ltc_divisor = 25,
  532. .height = 576,
  533. .width = 720,
  534. .sar = {{59, 54}, {118, 81}},
  535. .work_chunks = &work_chunks_dv50pal[0],
  536. .pix_fmt = PIX_FMT_YUV422P,
  537. .bpm = 6,
  538. .block_sizes = block_sizes_dv2550,
  539. .audio_stride = 108,
  540. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  541. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  542. .audio_shuffle = dv_audio_shuffle625,
  543. },
  544. { .dsf = 0,
  545. .video_stype = 0x14,
  546. .frame_size = 480000, /* SMPTE-370M - 1080i60 100 Mbps */
  547. .difseg_size = 10, /* also known as "DVCPRO HD" */
  548. .n_difchan = 4,
  549. .time_base = { 1001, 30000 },
  550. .ltc_divisor = 30,
  551. .height = 1080,
  552. .width = 1280,
  553. .sar = {{1, 1}, {1, 1}},
  554. .work_chunks = &work_chunks_dv100ntsci[0],
  555. .pix_fmt = PIX_FMT_YUV422P,
  556. .bpm = 8,
  557. .block_sizes = block_sizes_dv100,
  558. .audio_stride = 90,
  559. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  560. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  561. .audio_shuffle = dv_audio_shuffle525,
  562. },
  563. { .dsf = 1,
  564. .video_stype = 0x14,
  565. .frame_size = 576000, /* SMPTE-370M - 1080i50 100 Mbps */
  566. .difseg_size = 12, /* also known as "DVCPRO HD" */
  567. .n_difchan = 4,
  568. .time_base = { 1, 25 },
  569. .ltc_divisor = 25,
  570. .height = 1080,
  571. .width = 1440,
  572. .sar = {{1, 1}, {1, 1}},
  573. .work_chunks = &work_chunks_dv100pali[0],
  574. .pix_fmt = PIX_FMT_YUV422P,
  575. .bpm = 8,
  576. .block_sizes = block_sizes_dv100,
  577. .audio_stride = 108,
  578. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  579. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  580. .audio_shuffle = dv_audio_shuffle625,
  581. },
  582. { .dsf = 0,
  583. .video_stype = 0x18,
  584. .frame_size = 240000, /* SMPTE-370M - 720p60 100 Mbps */
  585. .difseg_size = 10, /* also known as "DVCPRO HD" */
  586. .n_difchan = 2,
  587. .time_base = { 1001, 60000 },
  588. .ltc_divisor = 60,
  589. .height = 720,
  590. .width = 960,
  591. .sar = {{1, 1}, {1, 1}},
  592. .work_chunks = &work_chunks_dv100ntscp[0],
  593. .pix_fmt = PIX_FMT_YUV422P,
  594. .bpm = 8,
  595. .block_sizes = block_sizes_dv100,
  596. .audio_stride = 90,
  597. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  598. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  599. .audio_shuffle = dv_audio_shuffle525,
  600. },
  601. { .dsf = 1,
  602. .video_stype = 0x18,
  603. .frame_size = 288000, /* SMPTE-370M - 720p50 100 Mbps */
  604. .difseg_size = 12, /* also known as "DVCPRO HD" */
  605. .n_difchan = 2,
  606. .time_base = { 1, 50 },
  607. .ltc_divisor = 50,
  608. .height = 720,
  609. .width = 960,
  610. .sar = {{1, 1}, {1, 1}},
  611. .work_chunks = &work_chunks_dv100palp[0],
  612. .pix_fmt = PIX_FMT_YUV422P,
  613. .bpm = 8,
  614. .block_sizes = block_sizes_dv100,
  615. .audio_stride = 90,
  616. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  617. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  618. .audio_shuffle = dv_audio_shuffle525,
  619. }
  620. };
  621. enum dv_section_type {
  622. dv_sect_header = 0x1f,
  623. dv_sect_subcode = 0x3f,
  624. dv_sect_vaux = 0x56,
  625. dv_sect_audio = 0x76,
  626. dv_sect_video = 0x96,
  627. };
  628. enum dv_pack_type {
  629. dv_header525 = 0x3f, /* see dv_write_pack for important details on */
  630. dv_header625 = 0xbf, /* these two packs */
  631. dv_timecode = 0x13,
  632. dv_audio_source = 0x50,
  633. dv_audio_control = 0x51,
  634. dv_audio_recdate = 0x52,
  635. dv_audio_rectime = 0x53,
  636. dv_video_source = 0x60,
  637. dv_video_control = 0x61,
  638. dv_video_recdate = 0x62,
  639. dv_video_rectime = 0x63,
  640. dv_unknown_pack = 0xff,
  641. };
  642. #define DV_PROFILE_IS_HD(p) ((p)->video_stype & 0x10)
  643. #define DV_PROFILE_IS_1080i50(p) (((p)->video_stype == 0x14) && ((p)->dsf == 1))
  644. #define DV_PROFILE_IS_720p50(p) (((p)->video_stype == 0x18) && ((p)->dsf == 1))
  645. /* minimum number of bytes to read from a DV stream in order to
  646. determine the profile */
  647. #define DV_PROFILE_BYTES (6*80) /* 6 DIF blocks */
  648. /**
  649. * largest possible DV frame, in bytes (1080i50)
  650. */
  651. #define DV_MAX_FRAME_SIZE 576000
  652. /**
  653. * maximum number of blocks per macroblock in any DV format
  654. */
  655. #define DV_MAX_BPM 8
  656. static inline const DVprofile* dv_frame_profile(const uint8_t* frame)
  657. {
  658. int i;
  659. int dsf = (frame[3] & 0x80) >> 7;
  660. int stype = frame[80*5 + 48 + 3] & 0x1f;
  661. /* 576i50 25Mbps 4:1:1 is a special case */
  662. if (dsf == 1 && stype == 0 && frame[5] & 0x07) {
  663. return &dv_profiles[2];
  664. }
  665. for (i=0; i<FF_ARRAY_ELEMS(dv_profiles); i++)
  666. if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)
  667. return &dv_profiles[i];
  668. return NULL;
  669. }
  670. static const DVprofile* dv_codec_profile(AVCodecContext* codec)
  671. {
  672. int i;
  673. for (i=0; i<FF_ARRAY_ELEMS(dv_profiles); i++)
  674. if (codec->height == dv_profiles[i].height &&
  675. codec->pix_fmt == dv_profiles[i].pix_fmt &&
  676. codec->width == dv_profiles[i].width)
  677. return &dv_profiles[i];
  678. return NULL;
  679. }
  680. static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
  681. uint8_t seq_num, uint8_t dif_num,
  682. uint8_t* buf)
  683. {
  684. buf[0] = (uint8_t)t; /* Section type */
  685. buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
  686. (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
  687. 7; /* reserved -- always 1 */
  688. buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
  689. return 3;
  690. }
  691. static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
  692. {
  693. if (syb_num == 0 || syb_num == 6) {
  694. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  695. (0 << 4) | /* AP3 (Subcode application ID) */
  696. 0x0f; /* reserved -- always 1 */
  697. }
  698. else if (syb_num == 11) {
  699. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  700. 0x7f; /* reserved -- always 1 */
  701. }
  702. else {
  703. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  704. (0 << 4) | /* APT (Track application ID) */
  705. 0x0f; /* reserved -- always 1 */
  706. }
  707. buf[1] = 0xf0 | /* reserved -- always 1 */
  708. (syb_num & 0x0f); /* SSYB number 0 - 11 */
  709. buf[2] = 0xff; /* reserved -- always 1 */
  710. return 3;
  711. }
  712. #endif /* AVCODEC_DVDATA_H */