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.

346 lines
12KB

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