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.

863 lines
26KB

  1. /*
  2. * AviSynth/AvxSynth support
  3. * Copyright (c) 2012 AvxSynth Team
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/internal.h"
  23. #include "libavcodec/internal.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "config.h"
  27. /* Enable function pointer definitions for runtime loading. */
  28. #define AVSC_NO_DECLSPEC
  29. /* Platform-specific directives for AviSynth vs AvxSynth. */
  30. #ifdef _WIN32
  31. #include <windows.h>
  32. #undef EXTERN_C
  33. #include <avisynth/avisynth_c.h>
  34. #define AVISYNTH_LIB "avisynth"
  35. #define USING_AVISYNTH
  36. #else
  37. #include <dlfcn.h>
  38. #include <avxsynth/avxsynth_c.h>
  39. #define AVISYNTH_NAME "libavxsynth"
  40. #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
  41. #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
  42. #define GetProcAddress dlsym
  43. #define FreeLibrary dlclose
  44. #endif
  45. typedef struct AviSynthLibrary {
  46. void *library;
  47. #define AVSC_DECLARE_FUNC(name) name ## _func name
  48. AVSC_DECLARE_FUNC(avs_bit_blt);
  49. AVSC_DECLARE_FUNC(avs_clip_get_error);
  50. AVSC_DECLARE_FUNC(avs_create_script_environment);
  51. AVSC_DECLARE_FUNC(avs_delete_script_environment);
  52. AVSC_DECLARE_FUNC(avs_get_audio);
  53. AVSC_DECLARE_FUNC(avs_get_error);
  54. AVSC_DECLARE_FUNC(avs_get_frame);
  55. AVSC_DECLARE_FUNC(avs_get_version);
  56. AVSC_DECLARE_FUNC(avs_get_video_info);
  57. AVSC_DECLARE_FUNC(avs_invoke);
  58. AVSC_DECLARE_FUNC(avs_release_clip);
  59. AVSC_DECLARE_FUNC(avs_release_value);
  60. AVSC_DECLARE_FUNC(avs_release_video_frame);
  61. AVSC_DECLARE_FUNC(avs_take_clip);
  62. #ifdef USING_AVISYNTH
  63. AVSC_DECLARE_FUNC(avs_bits_per_pixel);
  64. AVSC_DECLARE_FUNC(avs_get_height_p);
  65. AVSC_DECLARE_FUNC(avs_get_pitch_p);
  66. AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
  67. AVSC_DECLARE_FUNC(avs_get_row_size_p);
  68. AVSC_DECLARE_FUNC(avs_is_planar_rgb);
  69. AVSC_DECLARE_FUNC(avs_is_planar_rgba);
  70. #endif
  71. #undef AVSC_DECLARE_FUNC
  72. } AviSynthLibrary;
  73. typedef struct AviSynthContext {
  74. AVS_ScriptEnvironment *env;
  75. AVS_Clip *clip;
  76. const AVS_VideoInfo *vi;
  77. /* avisynth_read_packet_video() iterates over this. */
  78. int n_planes;
  79. const int *planes;
  80. int curr_stream;
  81. int curr_frame;
  82. int64_t curr_sample;
  83. int error;
  84. /* Linked list pointers. */
  85. struct AviSynthContext *next;
  86. } AviSynthContext;
  87. static const int avs_planes_packed[1] = { 0 };
  88. static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
  89. static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
  90. AVS_PLANAR_V };
  91. #ifdef USING_AVISYNTH
  92. static const int avs_planes_rgb[3] = { AVS_PLANAR_G, AVS_PLANAR_B,
  93. AVS_PLANAR_R };
  94. static const int avs_planes_yuva[4] = { AVS_PLANAR_Y, AVS_PLANAR_U,
  95. AVS_PLANAR_V, AVS_PLANAR_A };
  96. static const int avs_planes_rgba[4] = { AVS_PLANAR_G, AVS_PLANAR_B,
  97. AVS_PLANAR_R, AVS_PLANAR_A };
  98. #endif
  99. /* A conflict between C++ global objects, atexit, and dynamic loading requires
  100. * us to register our own atexit handler to prevent double freeing. */
  101. static AviSynthLibrary avs_library;
  102. static int avs_atexit_called = 0;
  103. /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
  104. static AviSynthContext *avs_ctx_list = NULL;
  105. static av_cold void avisynth_atexit_handler(void);
  106. static av_cold int avisynth_load_library(void)
  107. {
  108. avs_library.library = LoadLibrary(AVISYNTH_LIB);
  109. if (!avs_library.library)
  110. return AVERROR_UNKNOWN;
  111. #define LOAD_AVS_FUNC(name, continue_on_fail) \
  112. avs_library.name = (name ## _func) \
  113. GetProcAddress(avs_library.library, #name); \
  114. if (!continue_on_fail && !avs_library.name) \
  115. goto fail;
  116. LOAD_AVS_FUNC(avs_bit_blt, 0);
  117. LOAD_AVS_FUNC(avs_clip_get_error, 0);
  118. LOAD_AVS_FUNC(avs_create_script_environment, 0);
  119. LOAD_AVS_FUNC(avs_delete_script_environment, 0);
  120. LOAD_AVS_FUNC(avs_get_audio, 0);
  121. LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
  122. LOAD_AVS_FUNC(avs_get_frame, 0);
  123. LOAD_AVS_FUNC(avs_get_version, 0);
  124. LOAD_AVS_FUNC(avs_get_video_info, 0);
  125. LOAD_AVS_FUNC(avs_invoke, 0);
  126. LOAD_AVS_FUNC(avs_release_clip, 0);
  127. LOAD_AVS_FUNC(avs_release_value, 0);
  128. LOAD_AVS_FUNC(avs_release_video_frame, 0);
  129. LOAD_AVS_FUNC(avs_take_clip, 0);
  130. #ifdef USING_AVISYNTH
  131. LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
  132. LOAD_AVS_FUNC(avs_get_height_p, 1);
  133. LOAD_AVS_FUNC(avs_get_pitch_p, 1);
  134. LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
  135. LOAD_AVS_FUNC(avs_get_row_size_p, 1);
  136. LOAD_AVS_FUNC(avs_is_planar_rgb, 1);
  137. LOAD_AVS_FUNC(avs_is_planar_rgba, 1);
  138. #endif
  139. #undef LOAD_AVS_FUNC
  140. atexit(avisynth_atexit_handler);
  141. return 0;
  142. fail:
  143. FreeLibrary(avs_library.library);
  144. return AVERROR_UNKNOWN;
  145. }
  146. /* Note that avisynth_context_create and avisynth_context_destroy
  147. * do not allocate or free the actual context! That is taken care of
  148. * by libavformat. */
  149. static av_cold int avisynth_context_create(AVFormatContext *s)
  150. {
  151. AviSynthContext *avs = s->priv_data;
  152. int ret;
  153. if (!avs_library.library)
  154. if (ret = avisynth_load_library())
  155. return ret;
  156. avs->env = avs_library.avs_create_script_environment(3);
  157. if (avs_library.avs_get_error) {
  158. const char *error = avs_library.avs_get_error(avs->env);
  159. if (error) {
  160. av_log(s, AV_LOG_ERROR, "%s\n", error);
  161. return AVERROR_UNKNOWN;
  162. }
  163. }
  164. if (!avs_ctx_list) {
  165. avs_ctx_list = avs;
  166. } else {
  167. avs->next = avs_ctx_list;
  168. avs_ctx_list = avs;
  169. }
  170. return 0;
  171. }
  172. static av_cold void avisynth_context_destroy(AviSynthContext *avs)
  173. {
  174. if (avs_atexit_called)
  175. return;
  176. if (avs == avs_ctx_list) {
  177. avs_ctx_list = avs->next;
  178. } else {
  179. AviSynthContext *prev = avs_ctx_list;
  180. while (prev->next != avs)
  181. prev = prev->next;
  182. prev->next = avs->next;
  183. }
  184. if (avs->clip) {
  185. avs_library.avs_release_clip(avs->clip);
  186. avs->clip = NULL;
  187. }
  188. if (avs->env) {
  189. avs_library.avs_delete_script_environment(avs->env);
  190. avs->env = NULL;
  191. }
  192. }
  193. static av_cold void avisynth_atexit_handler(void)
  194. {
  195. AviSynthContext *avs = avs_ctx_list;
  196. while (avs) {
  197. AviSynthContext *next = avs->next;
  198. avisynth_context_destroy(avs);
  199. avs = next;
  200. }
  201. FreeLibrary(avs_library.library);
  202. avs_atexit_called = 1;
  203. }
  204. /* Create AVStream from audio and video data. */
  205. static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
  206. {
  207. AviSynthContext *avs = s->priv_data;
  208. int planar = 0; // 0: packed, 1: YUV, 2: Y8, 3: Planar RGB, 4: YUVA, 5: Planar RGBA
  209. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  210. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  211. st->codecpar->width = avs->vi->width;
  212. st->codecpar->height = avs->vi->height;
  213. st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
  214. avs->vi->fps_denominator };
  215. st->start_time = 0;
  216. st->duration = avs->vi->num_frames;
  217. st->nb_frames = avs->vi->num_frames;
  218. avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
  219. switch (avs->vi->pixel_type) {
  220. #ifdef USING_AVISYNTH
  221. /* 10~16-bit YUV pix_fmts (AviSynth+) */
  222. case AVS_CS_YUV444P10:
  223. st->codecpar->format = AV_PIX_FMT_YUV444P10;
  224. planar = 1;
  225. break;
  226. case AVS_CS_YUV422P10:
  227. st->codecpar->format = AV_PIX_FMT_YUV422P10;
  228. planar = 1;
  229. break;
  230. case AVS_CS_YUV420P10:
  231. st->codecpar->format = AV_PIX_FMT_YUV420P10;
  232. planar = 1;
  233. break;
  234. case AVS_CS_YUV444P12:
  235. st->codecpar->format = AV_PIX_FMT_YUV444P12;
  236. planar = 1;
  237. break;
  238. case AVS_CS_YUV422P12:
  239. st->codecpar->format = AV_PIX_FMT_YUV422P12;
  240. planar = 1;
  241. break;
  242. case AVS_CS_YUV420P12:
  243. st->codecpar->format = AV_PIX_FMT_YUV420P12;
  244. planar = 1;
  245. break;
  246. case AVS_CS_YUV444P16:
  247. st->codecpar->format = AV_PIX_FMT_YUV444P16;
  248. planar = 1;
  249. break;
  250. case AVS_CS_YUV422P16:
  251. st->codecpar->format = AV_PIX_FMT_YUV422P16;
  252. planar = 1;
  253. break;
  254. case AVS_CS_YUV420P16:
  255. st->codecpar->format = AV_PIX_FMT_YUV420P16;
  256. planar = 1;
  257. break;
  258. /* 8~16-bit YUV pix_fmts with Alpha (AviSynth+) */
  259. case AVS_CS_YUVA444:
  260. st->codecpar->format = AV_PIX_FMT_YUVA444P;
  261. planar = 4;
  262. break;
  263. case AVS_CS_YUVA422:
  264. st->codecpar->format = AV_PIX_FMT_YUVA422P;
  265. planar = 4;
  266. break;
  267. case AVS_CS_YUVA420:
  268. st->codecpar->format = AV_PIX_FMT_YUVA420P;
  269. planar = 4;
  270. break;
  271. case AVS_CS_YUVA444P10:
  272. st->codecpar->format = AV_PIX_FMT_YUVA444P10;
  273. planar = 4;
  274. break;
  275. case AVS_CS_YUVA422P10:
  276. st->codecpar->format = AV_PIX_FMT_YUVA422P10;
  277. planar = 4;
  278. break;
  279. case AVS_CS_YUVA420P10:
  280. st->codecpar->format = AV_PIX_FMT_YUVA420P10;
  281. planar = 4;
  282. break;
  283. case AVS_CS_YUVA444P16:
  284. st->codecpar->format = AV_PIX_FMT_YUVA444P16;
  285. planar = 4;
  286. break;
  287. case AVS_CS_YUVA422P16:
  288. st->codecpar->format = AV_PIX_FMT_YUVA422P16;
  289. planar = 4;
  290. break;
  291. case AVS_CS_YUVA420P16:
  292. st->codecpar->format = AV_PIX_FMT_YUVA420P16;
  293. planar = 4;
  294. break;
  295. /* Planar RGB pix_fmts (AviSynth+) */
  296. case AVS_CS_RGBP:
  297. st->codecpar->format = AV_PIX_FMT_GBRP;
  298. planar = 3;
  299. break;
  300. case AVS_CS_RGBP10:
  301. st->codecpar->format = AV_PIX_FMT_GBRP10;
  302. planar = 3;
  303. break;
  304. case AVS_CS_RGBP12:
  305. st->codecpar->format = AV_PIX_FMT_GBRP12;
  306. planar = 3;
  307. break;
  308. case AVS_CS_RGBP16:
  309. st->codecpar->format = AV_PIX_FMT_GBRP16;
  310. planar = 3;
  311. break;
  312. /* Planar RGB pix_fmts with Alpha (AviSynth+) */
  313. case AVS_CS_RGBAP:
  314. st->codecpar->format = AV_PIX_FMT_GBRAP;
  315. planar = 5;
  316. break;
  317. case AVS_CS_RGBAP12:
  318. st->codecpar->format = AV_PIX_FMT_GBRAP12;
  319. planar = 5;
  320. break;
  321. case AVS_CS_RGBAP16:
  322. st->codecpar->format = AV_PIX_FMT_GBRAP16;
  323. planar = 5;
  324. break;
  325. /* GRAY16 (AviSynth+) */
  326. case AVS_CS_Y16:
  327. st->codecpar->format = AV_PIX_FMT_GRAY16;
  328. planar = 2;
  329. break;
  330. /* pix_fmts added in AviSynth 2.6 */
  331. case AVS_CS_YV24:
  332. st->codecpar->format = AV_PIX_FMT_YUV444P;
  333. planar = 1;
  334. break;
  335. case AVS_CS_YV16:
  336. st->codecpar->format = AV_PIX_FMT_YUV422P;
  337. planar = 1;
  338. break;
  339. case AVS_CS_YV411:
  340. st->codecpar->format = AV_PIX_FMT_YUV411P;
  341. planar = 1;
  342. break;
  343. case AVS_CS_Y8:
  344. st->codecpar->format = AV_PIX_FMT_GRAY8;
  345. planar = 2;
  346. break;
  347. /* 16-bit packed RGB pix_fmts (AviSynth+) */
  348. case AVS_CS_BGR48:
  349. st->codecpar->format = AV_PIX_FMT_BGR48;
  350. break;
  351. case AVS_CS_BGR64:
  352. st->codecpar->format = AV_PIX_FMT_BGRA64;
  353. break;
  354. #endif
  355. /* AviSynth 2.5 and AvxSynth pix_fmts */
  356. case AVS_CS_BGR24:
  357. st->codecpar->format = AV_PIX_FMT_BGR24;
  358. break;
  359. case AVS_CS_BGR32:
  360. st->codecpar->format = AV_PIX_FMT_RGB32;
  361. break;
  362. case AVS_CS_YUY2:
  363. st->codecpar->format = AV_PIX_FMT_YUYV422;
  364. break;
  365. case AVS_CS_YV12:
  366. st->codecpar->format = AV_PIX_FMT_YUV420P;
  367. planar = 1;
  368. break;
  369. case AVS_CS_I420: // Is this even used anywhere?
  370. st->codecpar->format = AV_PIX_FMT_YUV420P;
  371. planar = 1;
  372. break;
  373. default:
  374. av_log(s, AV_LOG_ERROR,
  375. "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  376. avs->error = 1;
  377. return AVERROR_UNKNOWN;
  378. }
  379. switch (planar) {
  380. #ifdef USING_AVISYNTH
  381. case 5: // Planar RGB + Alpha
  382. avs->n_planes = 4;
  383. avs->planes = avs_planes_rgba;
  384. break;
  385. case 4: // YUV + Alpha
  386. avs->n_planes = 4;
  387. avs->planes = avs_planes_yuva;
  388. break;
  389. case 3: // Planar RGB
  390. avs->n_planes = 3;
  391. avs->planes = avs_planes_rgb;
  392. break;
  393. #endif
  394. case 2: // Y8
  395. avs->n_planes = 1;
  396. avs->planes = avs_planes_grey;
  397. break;
  398. case 1: // YUV
  399. avs->n_planes = 3;
  400. avs->planes = avs_planes_yuv;
  401. break;
  402. default:
  403. avs->n_planes = 1;
  404. avs->planes = avs_planes_packed;
  405. }
  406. return 0;
  407. }
  408. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
  409. {
  410. AviSynthContext *avs = s->priv_data;
  411. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  412. st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
  413. st->codecpar->channels = avs->vi->nchannels;
  414. st->duration = avs->vi->num_audio_samples;
  415. avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
  416. switch (avs->vi->sample_type) {
  417. case AVS_SAMPLE_INT8:
  418. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  419. break;
  420. case AVS_SAMPLE_INT16:
  421. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  422. break;
  423. case AVS_SAMPLE_INT24:
  424. st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
  425. break;
  426. case AVS_SAMPLE_INT32:
  427. st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
  428. break;
  429. case AVS_SAMPLE_FLOAT:
  430. st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
  431. break;
  432. default:
  433. av_log(s, AV_LOG_ERROR,
  434. "unknown AviSynth sample type %d\n", avs->vi->sample_type);
  435. avs->error = 1;
  436. return AVERROR_UNKNOWN;
  437. }
  438. return 0;
  439. }
  440. static int avisynth_create_stream(AVFormatContext *s)
  441. {
  442. AviSynthContext *avs = s->priv_data;
  443. AVStream *st;
  444. int ret;
  445. int id = 0;
  446. if (avs_has_video(avs->vi)) {
  447. st = avformat_new_stream(s, NULL);
  448. if (!st)
  449. return AVERROR_UNKNOWN;
  450. st->id = id++;
  451. if (ret = avisynth_create_stream_video(s, st))
  452. return ret;
  453. }
  454. if (avs_has_audio(avs->vi)) {
  455. st = avformat_new_stream(s, NULL);
  456. if (!st)
  457. return AVERROR_UNKNOWN;
  458. st->id = id++;
  459. if (ret = avisynth_create_stream_audio(s, st))
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. static int avisynth_open_file(AVFormatContext *s)
  465. {
  466. AviSynthContext *avs = s->priv_data;
  467. AVS_Value arg, val;
  468. int ret;
  469. #ifdef USING_AVISYNTH
  470. char filename_ansi[MAX_PATH * 4];
  471. wchar_t filename_wc[MAX_PATH * 4];
  472. #endif
  473. if (ret = avisynth_context_create(s))
  474. return ret;
  475. #ifdef USING_AVISYNTH
  476. /* Convert UTF-8 to ANSI code page */
  477. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
  478. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
  479. MAX_PATH * 4, NULL, NULL);
  480. arg = avs_new_value_string(filename_ansi);
  481. #else
  482. arg = avs_new_value_string(s->filename);
  483. #endif
  484. val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
  485. if (avs_is_error(val)) {
  486. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  487. ret = AVERROR_UNKNOWN;
  488. goto fail;
  489. }
  490. if (!avs_is_clip(val)) {
  491. av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
  492. ret = AVERROR_UNKNOWN;
  493. goto fail;
  494. }
  495. avs->clip = avs_library.avs_take_clip(val, avs->env);
  496. avs->vi = avs_library.avs_get_video_info(avs->clip);
  497. #ifdef USING_AVISYNTH
  498. /* On Windows, libav supports AviSynth interface version 6 or higher.
  499. * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
  500. * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
  501. * as interface version 3 like 2.5.8, this needs to be special-cased. */
  502. if (avs_library.avs_get_version(avs->clip) < 6) {
  503. av_log(s, AV_LOG_ERROR,
  504. "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
  505. ret = AVERROR_UNKNOWN;
  506. goto fail;
  507. }
  508. #endif
  509. /* Release the AVS_Value as it will go out of scope. */
  510. avs_library.avs_release_value(val);
  511. if (ret = avisynth_create_stream(s))
  512. goto fail;
  513. return 0;
  514. fail:
  515. avisynth_context_destroy(avs);
  516. return ret;
  517. }
  518. static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
  519. AVPacket *pkt, int *discard)
  520. {
  521. AviSynthContext *avs = s->priv_data;
  522. avs->curr_stream++;
  523. avs->curr_stream %= s->nb_streams;
  524. *st = s->streams[avs->curr_stream];
  525. if ((*st)->discard == AVDISCARD_ALL)
  526. *discard = 1;
  527. else
  528. *discard = 0;
  529. return;
  530. }
  531. /* Copy AviSynth clip data into an AVPacket. */
  532. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
  533. int discard)
  534. {
  535. AviSynthContext *avs = s->priv_data;
  536. AVS_VideoFrame *frame;
  537. unsigned char *dst_p;
  538. const unsigned char *src_p;
  539. int n, i, plane, rowsize, planeheight, pitch, bits;
  540. const char *error;
  541. int avsplus av_unused;
  542. if (avs->curr_frame >= avs->vi->num_frames)
  543. return AVERROR_EOF;
  544. /* This must happen even if the stream is discarded to prevent desync. */
  545. n = avs->curr_frame++;
  546. if (discard)
  547. return 0;
  548. #ifdef USING_AVISYNTH
  549. /* Detect whether we're using AviSynth 2.6 or AviSynth+ by
  550. * looking for whether avs_is_planar_rgb exists. */
  551. if (GetProcAddress(avs_library.library, "avs_is_planar_rgb") == NULL)
  552. avsplus = 0;
  553. else
  554. avsplus = 1;
  555. /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
  556. * requires going through avs_library, while AvxSynth has it under
  557. * the older AVSC_INLINE type, so special-case this. */
  558. bits = avs_library.avs_bits_per_pixel(avs->vi);
  559. #else
  560. bits = avs_bits_per_pixel(avs->vi);
  561. #endif
  562. /* Without the cast to int64_t, calculation overflows at about 9k x 9k
  563. * resolution. */
  564. pkt->size = (((int64_t)avs->vi->width *
  565. (int64_t)avs->vi->height) * bits) / 8;
  566. if (!pkt->size)
  567. return AVERROR_UNKNOWN;
  568. if (av_new_packet(pkt, pkt->size) < 0)
  569. return AVERROR(ENOMEM);
  570. pkt->pts = n;
  571. pkt->dts = n;
  572. pkt->duration = 1;
  573. pkt->stream_index = avs->curr_stream;
  574. frame = avs_library.avs_get_frame(avs->clip, n);
  575. error = avs_library.avs_clip_get_error(avs->clip);
  576. if (error) {
  577. av_log(s, AV_LOG_ERROR, "%s\n", error);
  578. avs->error = 1;
  579. av_packet_unref(pkt);
  580. return AVERROR_UNKNOWN;
  581. }
  582. dst_p = pkt->data;
  583. for (i = 0; i < avs->n_planes; i++) {
  584. plane = avs->planes[i];
  585. #ifdef USING_AVISYNTH
  586. src_p = avs_library.avs_get_read_ptr_p(frame, plane);
  587. pitch = avs_library.avs_get_pitch_p(frame, plane);
  588. rowsize = avs_library.avs_get_row_size_p(frame, plane);
  589. planeheight = avs_library.avs_get_height_p(frame, plane);
  590. #else
  591. src_p = avs_get_read_ptr_p(frame, plane);
  592. pitch = avs_get_pitch_p(frame, plane);
  593. rowsize = avs_get_row_size_p(frame, plane);
  594. planeheight = avs_get_height_p(frame, plane);
  595. #endif
  596. /* Flip RGB video. */
  597. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
  598. src_p = src_p + (planeheight - 1) * pitch;
  599. pitch = -pitch;
  600. }
  601. #ifdef USING_AVISYNTH
  602. /* Flip Planar RGB video */
  603. if (avsplus && (avs_library.avs_is_planar_rgb(avs->vi) ||
  604. avs_library.avs_is_planar_rgba(avs->vi))) {
  605. src_p = src_p + (planeheight - 1) * pitch;
  606. pitch = -pitch;
  607. }
  608. #endif
  609. avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
  610. rowsize, planeheight);
  611. dst_p += rowsize * planeheight;
  612. }
  613. avs_library.avs_release_video_frame(frame);
  614. return 0;
  615. }
  616. static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
  617. int discard)
  618. {
  619. AviSynthContext *avs = s->priv_data;
  620. AVRational fps, samplerate;
  621. int samples;
  622. int64_t n;
  623. const char *error;
  624. if (avs->curr_sample >= avs->vi->num_audio_samples)
  625. return AVERROR_EOF;
  626. fps.num = avs->vi->fps_numerator;
  627. fps.den = avs->vi->fps_denominator;
  628. samplerate.num = avs->vi->audio_samples_per_second;
  629. samplerate.den = 1;
  630. if (avs_has_video(avs->vi)) {
  631. if (avs->curr_frame < avs->vi->num_frames)
  632. samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
  633. avs->curr_sample;
  634. else
  635. samples = av_rescale_q(1, samplerate, fps);
  636. } else {
  637. samples = 1000;
  638. }
  639. /* After seeking, audio may catch up with video. */
  640. if (samples <= 0) {
  641. pkt->size = 0;
  642. pkt->data = NULL;
  643. return 0;
  644. }
  645. if (avs->curr_sample + samples > avs->vi->num_audio_samples)
  646. samples = avs->vi->num_audio_samples - avs->curr_sample;
  647. /* This must happen even if the stream is discarded to prevent desync. */
  648. n = avs->curr_sample;
  649. avs->curr_sample += samples;
  650. if (discard)
  651. return 0;
  652. pkt->size = avs_bytes_per_channel_sample(avs->vi) *
  653. samples * avs->vi->nchannels;
  654. if (!pkt->size)
  655. return AVERROR_UNKNOWN;
  656. if (av_new_packet(pkt, pkt->size) < 0)
  657. return AVERROR(ENOMEM);
  658. pkt->pts = n;
  659. pkt->dts = n;
  660. pkt->duration = samples;
  661. pkt->stream_index = avs->curr_stream;
  662. avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
  663. error = avs_library.avs_clip_get_error(avs->clip);
  664. if (error) {
  665. av_log(s, AV_LOG_ERROR, "%s\n", error);
  666. avs->error = 1;
  667. av_packet_unref(pkt);
  668. return AVERROR_UNKNOWN;
  669. }
  670. return 0;
  671. }
  672. static av_cold int avisynth_read_header(AVFormatContext *s)
  673. {
  674. int ret;
  675. // Calling library must implement a lock for thread-safe opens.
  676. if (ret = avpriv_lock_avformat())
  677. return ret;
  678. if (ret = avisynth_open_file(s)) {
  679. avpriv_unlock_avformat();
  680. return ret;
  681. }
  682. avpriv_unlock_avformat();
  683. return 0;
  684. }
  685. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  686. {
  687. AviSynthContext *avs = s->priv_data;
  688. AVStream *st;
  689. int discard = 0;
  690. int ret;
  691. if (avs->error)
  692. return AVERROR_UNKNOWN;
  693. /* If either stream reaches EOF, try to read the other one before
  694. * giving up. */
  695. avisynth_next_stream(s, &st, pkt, &discard);
  696. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  697. ret = avisynth_read_packet_video(s, pkt, discard);
  698. if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
  699. avisynth_next_stream(s, &st, pkt, &discard);
  700. return avisynth_read_packet_audio(s, pkt, discard);
  701. }
  702. } else {
  703. ret = avisynth_read_packet_audio(s, pkt, discard);
  704. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  705. avisynth_next_stream(s, &st, pkt, &discard);
  706. return avisynth_read_packet_video(s, pkt, discard);
  707. }
  708. }
  709. return ret;
  710. }
  711. static av_cold int avisynth_read_close(AVFormatContext *s)
  712. {
  713. if (avpriv_lock_avformat())
  714. return AVERROR_UNKNOWN;
  715. avisynth_context_destroy(s->priv_data);
  716. avpriv_unlock_avformat();
  717. return 0;
  718. }
  719. static int avisynth_read_seek(AVFormatContext *s, int stream_index,
  720. int64_t timestamp, int flags)
  721. {
  722. AviSynthContext *avs = s->priv_data;
  723. AVStream *st;
  724. AVRational fps, samplerate;
  725. if (avs->error)
  726. return AVERROR_UNKNOWN;
  727. fps = (AVRational) { avs->vi->fps_numerator,
  728. avs->vi->fps_denominator };
  729. samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
  730. st = s->streams[stream_index];
  731. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  732. /* AviSynth frame counts are signed int. */
  733. if ((timestamp >= avs->vi->num_frames) ||
  734. (timestamp > INT_MAX) ||
  735. (timestamp < 0))
  736. return AVERROR_EOF;
  737. avs->curr_frame = timestamp;
  738. if (avs_has_audio(avs->vi))
  739. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  740. } else {
  741. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  742. return AVERROR_EOF;
  743. /* Force frame granularity for seeking. */
  744. if (avs_has_video(avs->vi)) {
  745. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  746. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  747. } else {
  748. avs->curr_sample = timestamp;
  749. }
  750. }
  751. return 0;
  752. }
  753. AVInputFormat ff_avisynth_demuxer = {
  754. .name = "avisynth",
  755. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  756. .priv_data_size = sizeof(AviSynthContext),
  757. .read_header = avisynth_read_header,
  758. .read_packet = avisynth_read_packet,
  759. .read_close = avisynth_read_close,
  760. .read_seek = avisynth_read_seek,
  761. .extensions = "avs",
  762. };