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.

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