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.

332 lines
11KB

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