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.

333 lines
12KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include "libavutil/common.h"
  20. #include "libavutil/log.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "avcodec.h"
  23. #include "dv_profile.h"
  24. static DVwork_chunk work_chunks_dv25pal [1*12*27];
  25. static DVwork_chunk work_chunks_dv25pal411[1*12*27];
  26. static DVwork_chunk work_chunks_dv25ntsc [1*10*27];
  27. static DVwork_chunk work_chunks_dv50pal [2*12*27];
  28. static DVwork_chunk work_chunks_dv50ntsc [2*10*27];
  29. static DVwork_chunk work_chunks_dv100palp [2*12*27];
  30. static DVwork_chunk work_chunks_dv100ntscp[2*10*27];
  31. static DVwork_chunk work_chunks_dv100pali [4*12*27];
  32. static DVwork_chunk work_chunks_dv100ntsci[4*10*27];
  33. static uint32_t dv_idct_factor_sd [2*2*22*64];
  34. static uint32_t dv_idct_factor_hd1080[2*4*16*64];
  35. static uint32_t dv_idct_factor_hd720 [2*4*16*64];
  36. static const uint8_t dv_audio_shuffle525[10][9] = {
  37. { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
  38. { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
  39. { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
  40. { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
  41. { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
  42. { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
  43. { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
  44. { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
  45. { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
  46. { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
  47. };
  48. static const uint8_t dv_audio_shuffle625[12][9] = {
  49. { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */
  50. { 6, 42, 78, 32, 68, 104, 22, 58, 94},
  51. { 12, 48, 84, 2, 38, 74, 28, 64, 100},
  52. { 18, 54, 90, 8, 44, 80, 34, 70, 106},
  53. { 24, 60, 96, 14, 50, 86, 4, 40, 76},
  54. { 30, 66, 102, 20, 56, 92, 10, 46, 82},
  55. { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */
  56. { 7, 43, 79, 33, 69, 105, 23, 59, 95},
  57. { 13, 49, 85, 3, 39, 75, 29, 65, 101},
  58. { 19, 55, 91, 9, 45, 81, 35, 71, 107},
  59. { 25, 61, 97, 15, 51, 87, 5, 41, 77},
  60. { 31, 67, 103, 21, 57, 93, 11, 47, 83},
  61. };
  62. /* macroblock bit budgets */
  63. static const uint8_t block_sizes_dv2550[8] = {
  64. 112, 112, 112, 112, 80, 80, 0, 0,
  65. };
  66. static const uint8_t block_sizes_dv100[8] = {
  67. 80, 80, 80, 80, 80, 80, 64, 64,
  68. };
  69. static const DVprofile dv_profiles[] = {
  70. { .dsf = 0,
  71. .video_stype = 0x0,
  72. .frame_size = 120000, /* IEC 61834, SMPTE-314M - 525/60 (NTSC) */
  73. .difseg_size = 10,
  74. .n_difchan = 1,
  75. .time_base = { 1001, 30000 },
  76. .ltc_divisor = 30,
  77. .height = 480,
  78. .width = 720,
  79. .sar = {{8, 9}, {32, 27}},
  80. .work_chunks = &work_chunks_dv25ntsc[0],
  81. .idct_factor = &dv_idct_factor_sd[0],
  82. .pix_fmt = AV_PIX_FMT_YUV411P,
  83. .bpm = 6,
  84. .block_sizes = block_sizes_dv2550,
  85. .audio_stride = 90,
  86. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  87. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  88. .audio_shuffle = dv_audio_shuffle525,
  89. },
  90. { .dsf = 1,
  91. .video_stype = 0x0,
  92. .frame_size = 144000, /* IEC 61834 - 625/50 (PAL) */
  93. .difseg_size = 12,
  94. .n_difchan = 1,
  95. .time_base = { 1, 25 },
  96. .ltc_divisor = 25,
  97. .height = 576,
  98. .width = 720,
  99. .sar = {{16, 15}, {64, 45}},
  100. .work_chunks = &work_chunks_dv25pal[0],
  101. .idct_factor = &dv_idct_factor_sd[0],
  102. .pix_fmt = AV_PIX_FMT_YUV420P,
  103. .bpm = 6,
  104. .block_sizes = block_sizes_dv2550,
  105. .audio_stride = 108,
  106. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  107. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  108. .audio_shuffle = dv_audio_shuffle625,
  109. },
  110. { .dsf = 1,
  111. .video_stype = 0x0,
  112. .frame_size = 144000, /* SMPTE-314M - 625/50 (PAL) */
  113. .difseg_size = 12,
  114. .n_difchan = 1,
  115. .time_base = { 1, 25 },
  116. .ltc_divisor = 25,
  117. .height = 576,
  118. .width = 720,
  119. .sar = {{16, 15}, {64, 45}},
  120. .work_chunks = &work_chunks_dv25pal411[0],
  121. .idct_factor = &dv_idct_factor_sd[0],
  122. .pix_fmt = AV_PIX_FMT_YUV411P,
  123. .bpm = 6,
  124. .block_sizes = block_sizes_dv2550,
  125. .audio_stride = 108,
  126. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  127. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  128. .audio_shuffle = dv_audio_shuffle625,
  129. },
  130. { .dsf = 0,
  131. .video_stype = 0x4,
  132. .frame_size = 240000, /* SMPTE-314M - 525/60 (NTSC) 50 Mbps */
  133. .difseg_size = 10, /* also known as "DVCPRO50" */
  134. .n_difchan = 2,
  135. .time_base = { 1001, 30000 },
  136. .ltc_divisor = 30,
  137. .height = 480,
  138. .width = 720,
  139. .sar = {{8, 9}, {32, 27}},
  140. .work_chunks = &work_chunks_dv50ntsc[0],
  141. .idct_factor = &dv_idct_factor_sd[0],
  142. .pix_fmt = AV_PIX_FMT_YUV422P,
  143. .bpm = 6,
  144. .block_sizes = block_sizes_dv2550,
  145. .audio_stride = 90,
  146. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  147. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  148. .audio_shuffle = dv_audio_shuffle525,
  149. },
  150. { .dsf = 1,
  151. .video_stype = 0x4,
  152. .frame_size = 288000, /* SMPTE-314M - 625/50 (PAL) 50 Mbps */
  153. .difseg_size = 12, /* also known as "DVCPRO50" */
  154. .n_difchan = 2,
  155. .time_base = { 1, 25 },
  156. .ltc_divisor = 25,
  157. .height = 576,
  158. .width = 720,
  159. .sar = {{16, 15}, {64, 45}},
  160. .work_chunks = &work_chunks_dv50pal[0],
  161. .idct_factor = &dv_idct_factor_sd[0],
  162. .pix_fmt = AV_PIX_FMT_YUV422P,
  163. .bpm = 6,
  164. .block_sizes = block_sizes_dv2550,
  165. .audio_stride = 108,
  166. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  167. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  168. .audio_shuffle = dv_audio_shuffle625,
  169. },
  170. { .dsf = 0,
  171. .video_stype = 0x14,
  172. .frame_size = 480000, /* SMPTE-370M - 1080i60 100 Mbps */
  173. .difseg_size = 10, /* also known as "DVCPRO HD" */
  174. .n_difchan = 4,
  175. .time_base = { 1001, 30000 },
  176. .ltc_divisor = 30,
  177. .height = 1080,
  178. .width = 1280,
  179. .sar = {{1, 1}, {3, 2}},
  180. .work_chunks = &work_chunks_dv100ntsci[0],
  181. .idct_factor = &dv_idct_factor_hd1080[0],
  182. .pix_fmt = AV_PIX_FMT_YUV422P,
  183. .bpm = 8,
  184. .block_sizes = block_sizes_dv100,
  185. .audio_stride = 90,
  186. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  187. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  188. .audio_shuffle = dv_audio_shuffle525,
  189. },
  190. { .dsf = 1,
  191. .video_stype = 0x14,
  192. .frame_size = 576000, /* SMPTE-370M - 1080i50 100 Mbps */
  193. .difseg_size = 12, /* also known as "DVCPRO HD" */
  194. .n_difchan = 4,
  195. .time_base = { 1, 25 },
  196. .ltc_divisor = 25,
  197. .height = 1080,
  198. .width = 1440,
  199. .sar = {{1, 1}, {4, 3}},
  200. .work_chunks = &work_chunks_dv100pali[0],
  201. .idct_factor = &dv_idct_factor_hd1080[0],
  202. .pix_fmt = AV_PIX_FMT_YUV422P,
  203. .bpm = 8,
  204. .block_sizes = block_sizes_dv100,
  205. .audio_stride = 108,
  206. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  207. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  208. .audio_shuffle = dv_audio_shuffle625,
  209. },
  210. { .dsf = 0,
  211. .video_stype = 0x18,
  212. .frame_size = 240000, /* SMPTE-370M - 720p60 100 Mbps */
  213. .difseg_size = 10, /* also known as "DVCPRO HD" */
  214. .n_difchan = 2,
  215. .time_base = { 1001, 60000 },
  216. .ltc_divisor = 60,
  217. .height = 720,
  218. .width = 960,
  219. .sar = {{1, 1}, {4, 3}},
  220. .work_chunks = &work_chunks_dv100ntscp[0],
  221. .idct_factor = &dv_idct_factor_hd720[0],
  222. .pix_fmt = AV_PIX_FMT_YUV422P,
  223. .bpm = 8,
  224. .block_sizes = block_sizes_dv100,
  225. .audio_stride = 90,
  226. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  227. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  228. .audio_shuffle = dv_audio_shuffle525,
  229. },
  230. { .dsf = 1,
  231. .video_stype = 0x18,
  232. .frame_size = 288000, /* SMPTE-370M - 720p50 100 Mbps */
  233. .difseg_size = 12, /* also known as "DVCPRO HD" */
  234. .n_difchan = 2,
  235. .time_base = { 1, 50 },
  236. .ltc_divisor = 50,
  237. .height = 720,
  238. .width = 960,
  239. .sar = {{1, 1}, {4, 3}},
  240. .work_chunks = &work_chunks_dv100palp[0],
  241. .idct_factor = &dv_idct_factor_hd720[0],
  242. .pix_fmt = AV_PIX_FMT_YUV422P,
  243. .bpm = 8,
  244. .block_sizes = block_sizes_dv100,
  245. .audio_stride = 90,
  246. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  247. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  248. .audio_shuffle = dv_audio_shuffle625,
  249. },
  250. { .dsf = 1,
  251. .video_stype = 0x1,
  252. .frame_size = 144000, /* IEC 61883-5 - 625/50 (PAL) */
  253. .difseg_size = 12,
  254. .n_difchan = 1,
  255. .time_base = { 1, 25 },
  256. .ltc_divisor = 25,
  257. .height = 576,
  258. .width = 720,
  259. .sar = {{16, 15}, {64, 45}},
  260. .work_chunks = &work_chunks_dv25pal[0],
  261. .idct_factor = &dv_idct_factor_sd[0],
  262. .pix_fmt = AV_PIX_FMT_YUV420P,
  263. .bpm = 6,
  264. .block_sizes = block_sizes_dv2550,
  265. .audio_stride = 108,
  266. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  267. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  268. .audio_shuffle = dv_audio_shuffle625,
  269. }
  270. };
  271. const DVprofile* avpriv_dv_frame_profile(const DVprofile *sys,
  272. const uint8_t* frame, unsigned buf_size)
  273. {
  274. int i, dsf, stype;
  275. if (buf_size < 80 * 5 + 48 + 4)
  276. return NULL;
  277. dsf = (frame[3] & 0x80) >> 7;
  278. stype = frame[80 * 5 + 48 + 3] & 0x1f;
  279. /* 576i50 25Mbps 4:1:1 is a special case */
  280. if (dsf == 1 && stype == 0 && frame[4] & 0x07 /* the APT field */) {
  281. return &dv_profiles[2];
  282. }
  283. for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)
  284. if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)
  285. return &dv_profiles[i];
  286. /* check if old sys matches and assumes corrupted input */
  287. if (sys && buf_size == sys->frame_size)
  288. return sys;
  289. return NULL;
  290. }
  291. const DVprofile* avpriv_dv_codec_profile(AVCodecContext* codec)
  292. {
  293. int i;
  294. for (i=0; i<FF_ARRAY_ELEMS(dv_profiles); i++)
  295. if (codec->height == dv_profiles[i].height &&
  296. codec->pix_fmt == dv_profiles[i].pix_fmt &&
  297. codec->width == dv_profiles[i].width)
  298. return &dv_profiles[i];
  299. return NULL;
  300. }
  301. void ff_dv_print_profiles(void *logctx, int loglevel)
  302. {
  303. int i;
  304. for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++) {
  305. const DVprofile *p = &dv_profiles[i];
  306. av_log(logctx, loglevel, "Frame size: %dx%d; pixel format: %s, "
  307. "framerate: %d/%d\n", p->width, p->height, av_get_pix_fmt_name(p->pix_fmt),
  308. p->time_base.den, p->time_base.num);
  309. }
  310. }