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.

360 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. #include "libavutil/rational.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avcodec.h"
  29. #include "dvdata.h"
  30. static DVwork_chunk work_chunks_dv25pal [1*12*27];
  31. static DVwork_chunk work_chunks_dv25pal411[1*12*27];
  32. static DVwork_chunk work_chunks_dv25ntsc [1*10*27];
  33. static DVwork_chunk work_chunks_dv50pal [2*12*27];
  34. static DVwork_chunk work_chunks_dv50ntsc [2*10*27];
  35. static DVwork_chunk work_chunks_dv100palp [2*12*27];
  36. static DVwork_chunk work_chunks_dv100ntscp[2*10*27];
  37. static DVwork_chunk work_chunks_dv100pali [4*12*27];
  38. static DVwork_chunk work_chunks_dv100ntsci[4*10*27];
  39. static uint32_t dv_idct_factor_sd [2*2*22*64];
  40. static uint32_t dv_idct_factor_hd1080[2*4*16*64];
  41. static uint32_t dv_idct_factor_hd720 [2*4*16*64];
  42. static const uint8_t dv_audio_shuffle525[10][9] = {
  43. { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
  44. { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
  45. { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
  46. { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
  47. { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
  48. { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
  49. { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
  50. { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
  51. { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
  52. { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
  53. };
  54. static const uint8_t dv_audio_shuffle625[12][9] = {
  55. { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */
  56. { 6, 42, 78, 32, 68, 104, 22, 58, 94},
  57. { 12, 48, 84, 2, 38, 74, 28, 64, 100},
  58. { 18, 54, 90, 8, 44, 80, 34, 70, 106},
  59. { 24, 60, 96, 14, 50, 86, 4, 40, 76},
  60. { 30, 66, 102, 20, 56, 92, 10, 46, 82},
  61. { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */
  62. { 7, 43, 79, 33, 69, 105, 23, 59, 95},
  63. { 13, 49, 85, 3, 39, 75, 29, 65, 101},
  64. { 19, 55, 91, 9, 45, 81, 35, 71, 107},
  65. { 25, 61, 97, 15, 51, 87, 5, 41, 77},
  66. { 31, 67, 103, 21, 57, 93, 11, 47, 83},
  67. };
  68. /* macroblock bit budgets */
  69. static const uint8_t block_sizes_dv2550[8] = {
  70. 112, 112, 112, 112, 80, 80, 0, 0,
  71. };
  72. static const uint8_t block_sizes_dv100[8] = {
  73. 80, 80, 80, 80, 80, 80, 64, 64,
  74. };
  75. static const DVprofile dv_profiles[] = {
  76. { .dsf = 0,
  77. .video_stype = 0x0,
  78. .frame_size = 120000, /* IEC 61834, SMPTE-314M - 525/60 (NTSC) */
  79. .difseg_size = 10,
  80. .n_difchan = 1,
  81. .time_base = { 1001, 30000 },
  82. .ltc_divisor = 30,
  83. .height = 480,
  84. .width = 720,
  85. .sar = {{8, 9}, {32, 27}},
  86. .work_chunks = &work_chunks_dv25ntsc[0],
  87. .idct_factor = &dv_idct_factor_sd[0],
  88. .pix_fmt = PIX_FMT_YUV411P,
  89. .bpm = 6,
  90. .block_sizes = block_sizes_dv2550,
  91. .audio_stride = 90,
  92. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  93. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  94. .audio_shuffle = dv_audio_shuffle525,
  95. },
  96. { .dsf = 1,
  97. .video_stype = 0x0,
  98. .frame_size = 144000, /* IEC 61834 - 625/50 (PAL) */
  99. .difseg_size = 12,
  100. .n_difchan = 1,
  101. .time_base = { 1, 25 },
  102. .ltc_divisor = 25,
  103. .height = 576,
  104. .width = 720,
  105. .sar = {{16, 15}, {64, 45}},
  106. .work_chunks = &work_chunks_dv25pal[0],
  107. .idct_factor = &dv_idct_factor_sd[0],
  108. .pix_fmt = PIX_FMT_YUV420P,
  109. .bpm = 6,
  110. .block_sizes = block_sizes_dv2550,
  111. .audio_stride = 108,
  112. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  113. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  114. .audio_shuffle = dv_audio_shuffle625,
  115. },
  116. { .dsf = 1,
  117. .video_stype = 0x0,
  118. .frame_size = 144000, /* SMPTE-314M - 625/50 (PAL) */
  119. .difseg_size = 12,
  120. .n_difchan = 1,
  121. .time_base = { 1, 25 },
  122. .ltc_divisor = 25,
  123. .height = 576,
  124. .width = 720,
  125. .sar = {{16, 15}, {64, 45}},
  126. .work_chunks = &work_chunks_dv25pal411[0],
  127. .idct_factor = &dv_idct_factor_sd[0],
  128. .pix_fmt = PIX_FMT_YUV411P,
  129. .bpm = 6,
  130. .block_sizes = block_sizes_dv2550,
  131. .audio_stride = 108,
  132. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  133. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  134. .audio_shuffle = dv_audio_shuffle625,
  135. },
  136. { .dsf = 0,
  137. .video_stype = 0x4,
  138. .frame_size = 240000, /* SMPTE-314M - 525/60 (NTSC) 50 Mbps */
  139. .difseg_size = 10, /* also known as "DVCPRO50" */
  140. .n_difchan = 2,
  141. .time_base = { 1001, 30000 },
  142. .ltc_divisor = 30,
  143. .height = 480,
  144. .width = 720,
  145. .sar = {{8, 9}, {32, 27}},
  146. .work_chunks = &work_chunks_dv50ntsc[0],
  147. .idct_factor = &dv_idct_factor_sd[0],
  148. .pix_fmt = PIX_FMT_YUV422P,
  149. .bpm = 6,
  150. .block_sizes = block_sizes_dv2550,
  151. .audio_stride = 90,
  152. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  153. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  154. .audio_shuffle = dv_audio_shuffle525,
  155. },
  156. { .dsf = 1,
  157. .video_stype = 0x4,
  158. .frame_size = 288000, /* SMPTE-314M - 625/50 (PAL) 50 Mbps */
  159. .difseg_size = 12, /* also known as "DVCPRO50" */
  160. .n_difchan = 2,
  161. .time_base = { 1, 25 },
  162. .ltc_divisor = 25,
  163. .height = 576,
  164. .width = 720,
  165. .sar = {{16, 15}, {64, 45}},
  166. .work_chunks = &work_chunks_dv50pal[0],
  167. .idct_factor = &dv_idct_factor_sd[0],
  168. .pix_fmt = PIX_FMT_YUV422P,
  169. .bpm = 6,
  170. .block_sizes = block_sizes_dv2550,
  171. .audio_stride = 108,
  172. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  173. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  174. .audio_shuffle = dv_audio_shuffle625,
  175. },
  176. { .dsf = 0,
  177. .video_stype = 0x14,
  178. .frame_size = 480000, /* SMPTE-370M - 1080i60 100 Mbps */
  179. .difseg_size = 10, /* also known as "DVCPRO HD" */
  180. .n_difchan = 4,
  181. .time_base = { 1001, 30000 },
  182. .ltc_divisor = 30,
  183. .height = 1080,
  184. .width = 1280,
  185. .sar = {{1, 1}, {3, 2}},
  186. .work_chunks = &work_chunks_dv100ntsci[0],
  187. .idct_factor = &dv_idct_factor_hd1080[0],
  188. .pix_fmt = PIX_FMT_YUV422P,
  189. .bpm = 8,
  190. .block_sizes = block_sizes_dv100,
  191. .audio_stride = 90,
  192. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  193. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  194. .audio_shuffle = dv_audio_shuffle525,
  195. },
  196. { .dsf = 1,
  197. .video_stype = 0x14,
  198. .frame_size = 576000, /* SMPTE-370M - 1080i50 100 Mbps */
  199. .difseg_size = 12, /* also known as "DVCPRO HD" */
  200. .n_difchan = 4,
  201. .time_base = { 1, 25 },
  202. .ltc_divisor = 25,
  203. .height = 1080,
  204. .width = 1440,
  205. .sar = {{1, 1}, {4, 3}},
  206. .work_chunks = &work_chunks_dv100pali[0],
  207. .idct_factor = &dv_idct_factor_hd1080[0],
  208. .pix_fmt = PIX_FMT_YUV422P,
  209. .bpm = 8,
  210. .block_sizes = block_sizes_dv100,
  211. .audio_stride = 108,
  212. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  213. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  214. .audio_shuffle = dv_audio_shuffle625,
  215. },
  216. { .dsf = 0,
  217. .video_stype = 0x18,
  218. .frame_size = 240000, /* SMPTE-370M - 720p60 100 Mbps */
  219. .difseg_size = 10, /* also known as "DVCPRO HD" */
  220. .n_difchan = 2,
  221. .time_base = { 1001, 60000 },
  222. .ltc_divisor = 60,
  223. .height = 720,
  224. .width = 960,
  225. .sar = {{1, 1}, {4, 3}},
  226. .work_chunks = &work_chunks_dv100ntscp[0],
  227. .idct_factor = &dv_idct_factor_hd720[0],
  228. .pix_fmt = PIX_FMT_YUV422P,
  229. .bpm = 8,
  230. .block_sizes = block_sizes_dv100,
  231. .audio_stride = 90,
  232. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  233. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  234. .audio_shuffle = dv_audio_shuffle525,
  235. },
  236. { .dsf = 1,
  237. .video_stype = 0x18,
  238. .frame_size = 288000, /* SMPTE-370M - 720p50 100 Mbps */
  239. .difseg_size = 12, /* also known as "DVCPRO HD" */
  240. .n_difchan = 2,
  241. .time_base = { 1, 50 },
  242. .ltc_divisor = 50,
  243. .height = 720,
  244. .width = 960,
  245. .sar = {{1, 1}, {4, 3}},
  246. .work_chunks = &work_chunks_dv100palp[0],
  247. .idct_factor = &dv_idct_factor_hd720[0],
  248. .pix_fmt = PIX_FMT_YUV422P,
  249. .bpm = 8,
  250. .block_sizes = block_sizes_dv100,
  251. .audio_stride = 90,
  252. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  253. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  254. .audio_shuffle = dv_audio_shuffle625,
  255. },
  256. { .dsf = 1,
  257. .video_stype = 0x1,
  258. .frame_size = 144000, /* IEC 61883-5 - 625/50 (PAL) */
  259. .difseg_size = 12,
  260. .n_difchan = 1,
  261. .time_base = { 1, 25 },
  262. .ltc_divisor = 25,
  263. .height = 576,
  264. .width = 720,
  265. .sar = {{16, 15}, {64, 45}},
  266. .work_chunks = &work_chunks_dv25pal[0],
  267. .idct_factor = &dv_idct_factor_sd[0],
  268. .pix_fmt = PIX_FMT_YUV420P,
  269. .bpm = 6,
  270. .block_sizes = block_sizes_dv2550,
  271. .audio_stride = 108,
  272. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  273. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  274. .audio_shuffle = dv_audio_shuffle625,
  275. }
  276. };
  277. const DVprofile* avpriv_dv_frame_profile2(AVCodecContext* codec, const DVprofile *sys,
  278. const uint8_t* frame, unsigned buf_size)
  279. {
  280. int i, dsf, stype;
  281. if(buf_size < DV_PROFILE_BYTES)
  282. return NULL;
  283. dsf = (frame[3] & 0x80) >> 7;
  284. stype = frame[80 * 5 + 48 + 3] & 0x1f;
  285. /* 576i50 25Mbps 4:1:1 is a special case */
  286. if (dsf == 1 && stype == 0 && frame[4] & 0x07 /* the APT field */) {
  287. return &dv_profiles[2];
  288. }
  289. if(stype == 0 && codec && codec->codec_tag==AV_RL32("dvsd") && codec->coded_width==720 && codec->coded_height==576)
  290. return &dv_profiles[1];
  291. for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)
  292. if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)
  293. return &dv_profiles[i];
  294. /* check if old sys matches and assumes corrupted input */
  295. if (sys && buf_size == sys->frame_size)
  296. return sys;
  297. /* hack for trac issue #217, dv files created with QuickTime 3 */
  298. if ((frame[3] & 0x7f) == 0x3f && frame[80 * 5 + 48 + 3] == 0xff)
  299. return &dv_profiles[dsf];
  300. return NULL;
  301. }
  302. const DVprofile* avpriv_dv_frame_profile(const DVprofile *sys,
  303. const uint8_t* frame, unsigned buf_size)
  304. {
  305. return avpriv_dv_frame_profile2(NULL, sys, frame, buf_size);
  306. }
  307. const DVprofile* avpriv_dv_codec_profile(AVCodecContext* codec)
  308. {
  309. int i;
  310. int w, h;
  311. if (codec->coded_width || codec->coded_height) {
  312. w = codec->coded_width;
  313. h = codec->coded_height;
  314. } else {
  315. w = codec->width;
  316. h = codec->height;
  317. }
  318. for (i=0; i<FF_ARRAY_ELEMS(dv_profiles); i++)
  319. if (h == dv_profiles[i].height &&
  320. codec->pix_fmt == dv_profiles[i].pix_fmt &&
  321. w == dv_profiles[i].width)
  322. return &dv_profiles[i];
  323. return NULL;
  324. }
  325. void ff_dv_print_profiles(void *logctx, int loglevel)
  326. {
  327. int i;
  328. for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++) {
  329. const DVprofile *p = &dv_profiles[i];
  330. av_log(logctx, loglevel, "Frame size: %dx%d; pixel format: %s, "
  331. "framerate: %d/%d\n", p->width, p->height, av_get_pix_fmt_name(p->pix_fmt),
  332. p->time_base.den, p->time_base.num);
  333. }
  334. }