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.

878 lines
26KB

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