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.

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