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.

327 lines
11KB

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