dv_profile.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 "config.h"
  19. #include <stdint.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. { .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. { .dsf = 1,
  97. .video_stype = 0x0,
  98. .frame_size = 144000, /* SMPTE-314M - 625/50 (PAL) */
  99. .difseg_size = 12,
  100. .n_difchan = 1,
  101. .time_base = { 1, 25 },
  102. .ltc_divisor = 25,
  103. .height = 576,
  104. .width = 720,
  105. .sar = { { 16, 15 }, { 64, 45 } },
  106. .pix_fmt = AV_PIX_FMT_YUV411P,
  107. .bpm = 6,
  108. .block_sizes = block_sizes_dv2550,
  109. .audio_stride = 108,
  110. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  111. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  112. .audio_shuffle = dv_audio_shuffle625, },
  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. { .dsf = 1,
  131. .video_stype = 0x4,
  132. .frame_size = 288000, /* SMPTE-314M - 625/50 (PAL) 50 Mbps */
  133. .difseg_size = 12, /* also known as "DVCPRO50" */
  134. .n_difchan = 2,
  135. .time_base = { 1, 25 },
  136. .ltc_divisor = 25,
  137. .height = 576,
  138. .width = 720,
  139. .sar = { { 16, 15 }, { 64, 45 } },
  140. .pix_fmt = AV_PIX_FMT_YUV422P,
  141. .bpm = 6,
  142. .block_sizes = block_sizes_dv2550,
  143. .audio_stride = 108,
  144. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  145. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  146. .audio_shuffle = dv_audio_shuffle625, },
  147. { .dsf = 0,
  148. .video_stype = 0x14,
  149. .frame_size = 480000, /* SMPTE-370M - 1080i60 100 Mbps */
  150. .difseg_size = 10, /* also known as "DVCPRO HD" */
  151. .n_difchan = 4,
  152. .time_base = { 1001, 30000 },
  153. .ltc_divisor = 30,
  154. .height = 1080,
  155. .width = 1280,
  156. .sar = { { 1, 1 }, { 3, 2 } },
  157. .pix_fmt = AV_PIX_FMT_YUV422P,
  158. .bpm = 8,
  159. .block_sizes = block_sizes_dv100,
  160. .audio_stride = 90,
  161. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  162. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  163. .audio_shuffle = dv_audio_shuffle525, },
  164. { .dsf = 1,
  165. .video_stype = 0x14,
  166. .frame_size = 576000, /* SMPTE-370M - 1080i50 100 Mbps */
  167. .difseg_size = 12, /* also known as "DVCPRO HD" */
  168. .n_difchan = 4,
  169. .time_base = { 1, 25 },
  170. .ltc_divisor = 25,
  171. .height = 1080,
  172. .width = 1440,
  173. .sar = { { 1, 1 }, { 4, 3 } },
  174. .pix_fmt = AV_PIX_FMT_YUV422P,
  175. .bpm = 8,
  176. .block_sizes = block_sizes_dv100,
  177. .audio_stride = 108,
  178. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  179. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  180. .audio_shuffle = dv_audio_shuffle625, },
  181. { .dsf = 0,
  182. .video_stype = 0x18,
  183. .frame_size = 240000, /* SMPTE-370M - 720p60 100 Mbps */
  184. .difseg_size = 10, /* also known as "DVCPRO HD" */
  185. .n_difchan = 2,
  186. .time_base = { 1001, 60000 },
  187. .ltc_divisor = 60,
  188. .height = 720,
  189. .width = 960,
  190. .sar = { { 1, 1 }, { 4, 3 } },
  191. .pix_fmt = AV_PIX_FMT_YUV422P,
  192. .bpm = 8,
  193. .block_sizes = block_sizes_dv100,
  194. .audio_stride = 90,
  195. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32kHz */
  196. .audio_samples_dist = { 1600, 1602, 1602, 1602, 1602 }, /* per SMPTE-314M */
  197. .audio_shuffle = dv_audio_shuffle525, },
  198. { .dsf = 1,
  199. .video_stype = 0x18,
  200. .frame_size = 288000, /* SMPTE-370M - 720p50 100 Mbps */
  201. .difseg_size = 12, /* also known as "DVCPRO HD" */
  202. .n_difchan = 2,
  203. .time_base = { 1, 50 },
  204. .ltc_divisor = 50,
  205. .height = 720,
  206. .width = 960,
  207. .sar = { { 1, 1 }, { 4, 3 } },
  208. .pix_fmt = AV_PIX_FMT_YUV422P,
  209. .bpm = 8,
  210. .block_sizes = block_sizes_dv100,
  211. .audio_stride = 90,
  212. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  213. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  214. .audio_shuffle = dv_audio_shuffle625, },
  215. { .dsf = 1,
  216. .video_stype = 0x1,
  217. .frame_size = 144000, /* IEC 61883-5 - 625/50 (PAL) */
  218. .difseg_size = 12,
  219. .n_difchan = 1,
  220. .time_base = { 1, 25 },
  221. .ltc_divisor = 25,
  222. .height = 576,
  223. .width = 720,
  224. .sar = { { 16, 15 }, { 64, 45 } },
  225. .pix_fmt = AV_PIX_FMT_YUV420P,
  226. .bpm = 6,
  227. .block_sizes = block_sizes_dv2550,
  228. .audio_stride = 108,
  229. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32kHz */
  230. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  231. .audio_shuffle = dv_audio_shuffle625, }
  232. };
  233. void ff_dv_print_profiles(void *logctx, int loglevel)
  234. {
  235. int i;
  236. for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++) {
  237. const AVDVProfile *p = &dv_profiles[i];
  238. av_log(logctx, loglevel,
  239. "Frame size: %dx%d; pixel format: %s, framerate: %d/%d\n",
  240. p->width, p->height, av_get_pix_fmt_name(p->pix_fmt),
  241. p->time_base.den, p->time_base.num);
  242. }
  243. }
  244. #endif /* CONFIG_DVPROFILE */
  245. const AVDVProfile* ff_dv_frame_profile(AVCodecContext* codec, const AVDVProfile *sys,
  246. const uint8_t *frame, unsigned buf_size)
  247. {
  248. #if CONFIG_DVPROFILE
  249. int i, dsf, stype;
  250. if(buf_size < DV_PROFILE_BYTES)
  251. return NULL;
  252. dsf = (frame[3] & 0x80) >> 7;
  253. stype = frame[80 * 5 + 48 + 3] & 0x1f;
  254. /* 576i50 25Mbps 4:1:1 is a special case */
  255. if ((dsf == 1 && stype == 0 && frame[4] & 0x07 /* the APT field */) ||
  256. (stype == 31 && codec && codec->codec_tag==AV_RL32("SL25") && codec->coded_width==720 && codec->coded_height==576))
  257. return &dv_profiles[2];
  258. if( stype == 0
  259. && codec
  260. && (codec->codec_tag==AV_RL32("dvsd") || codec->codec_tag==AV_RL32("CDVC"))
  261. && codec->coded_width ==720
  262. && codec->coded_height==576)
  263. return &dv_profiles[1];
  264. for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)
  265. if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)
  266. return &dv_profiles[i];
  267. /* check if old sys matches and assumes corrupted input */
  268. if (sys && buf_size == sys->frame_size)
  269. return sys;
  270. /* hack for trac issue #217, dv files created with QuickTime 3 */
  271. if ((frame[3] & 0x7f) == 0x3f && frame[80 * 5 + 48 + 3] == 0xff)
  272. return &dv_profiles[dsf];
  273. #endif
  274. return NULL;
  275. }
  276. const AVDVProfile *av_dv_frame_profile(const AVDVProfile *sys,
  277. const uint8_t *frame, unsigned buf_size)
  278. {
  279. return ff_dv_frame_profile(NULL, sys, frame, buf_size);
  280. }
  281. const AVDVProfile *av_dv_codec_profile(int width, int height,
  282. enum AVPixelFormat pix_fmt)
  283. {
  284. #if CONFIG_DVPROFILE
  285. return av_dv_codec_profile2(width, height, pix_fmt, (AVRational){0, 0});
  286. #endif
  287. return NULL;
  288. }
  289. const AVDVProfile *av_dv_codec_profile2(int width, int height,
  290. enum AVPixelFormat pix_fmt,
  291. AVRational frame_rate)
  292. {
  293. const AVDVProfile *p = NULL;
  294. #if CONFIG_DVPROFILE
  295. int i;
  296. /* frame rate is necessary to select between 720p50 and 720p60 profiles */
  297. int invalid_framerate = frame_rate.num == 0 || frame_rate.den == 0;
  298. for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)
  299. if (height == dv_profiles[i].height &&
  300. pix_fmt == dv_profiles[i].pix_fmt &&
  301. width == dv_profiles[i].width)
  302. {
  303. if( invalid_framerate || av_div_q(dv_profiles[i].time_base, frame_rate).num == 1 )
  304. return &dv_profiles[i];
  305. if(!p)
  306. p = &dv_profiles[i];
  307. }
  308. #endif
  309. return p;
  310. }