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.

361 lines
13KB

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