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.

718 lines
21KB

  1. /*
  2. * AviSynth/AvxSynth support
  3. * Copyright (c) 2012 AvxSynth Team.
  4. *
  5. * This file is part of FFmpeg
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/internal.h"
  21. #include "libavcodec/internal.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. /* Enable function pointer definitions for runtime loading. */
  25. #define AVSC_NO_DECLSPEC
  26. /* Platform-specific directives for AviSynth vs AvxSynth. */
  27. #ifdef _WIN32
  28. #include <windows.h>
  29. #undef EXTERN_C
  30. #include "compat/avisynth/avisynth_c.h"
  31. #define AVISYNTH_LIB "avisynth"
  32. #define USING_AVISYNTH
  33. #else
  34. #include <dlfcn.h>
  35. #include "compat/avisynth/avxsynth_c.h"
  36. #if defined (__APPLE__)
  37. #define AVISYNTH_LIB "libavxsynth.dylib"
  38. #else
  39. #define AVISYNTH_LIB "libavxsynth.so"
  40. #endif
  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_yv24);
  69. AVSC_DECLARE_FUNC(avs_is_yv16);
  70. AVSC_DECLARE_FUNC(avs_is_yv411);
  71. AVSC_DECLARE_FUNC(avs_is_y8);
  72. #endif
  73. #undef AVSC_DECLARE_FUNC
  74. } AviSynthLibrary;
  75. typedef struct AviSynthContext {
  76. AVS_ScriptEnvironment *env;
  77. AVS_Clip *clip;
  78. const AVS_VideoInfo *vi;
  79. /* avisynth_read_packet_video() iterates over this. */
  80. int n_planes;
  81. const int *planes;
  82. int curr_stream;
  83. int curr_frame;
  84. int64_t curr_sample;
  85. int error;
  86. /* Linked list pointers. */
  87. struct AviSynthContext *next;
  88. } AviSynthContext;
  89. static const int avs_planes_packed[1] = { 0 };
  90. static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
  91. static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
  92. AVS_PLANAR_V };
  93. /* A conflict between C++ global objects, atexit, and dynamic loading requires
  94. * us to register our own atexit handler to prevent double freeing. */
  95. static AviSynthLibrary avs_library;
  96. static int avs_atexit_called = 0;
  97. /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
  98. static AviSynthContext *avs_ctx_list = NULL;
  99. static av_cold void avisynth_atexit_handler(void);
  100. static av_cold int avisynth_load_library(void)
  101. {
  102. avs_library.library = LoadLibrary(AVISYNTH_LIB);
  103. if (!avs_library.library)
  104. return AVERROR_UNKNOWN;
  105. #define LOAD_AVS_FUNC(name, continue_on_fail) \
  106. avs_library.name = \
  107. (void *)GetProcAddress(avs_library.library, #name); \
  108. if (!continue_on_fail && !avs_library.name) \
  109. goto fail;
  110. LOAD_AVS_FUNC(avs_bit_blt, 0);
  111. LOAD_AVS_FUNC(avs_clip_get_error, 0);
  112. LOAD_AVS_FUNC(avs_create_script_environment, 0);
  113. LOAD_AVS_FUNC(avs_delete_script_environment, 0);
  114. LOAD_AVS_FUNC(avs_get_audio, 0);
  115. LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
  116. LOAD_AVS_FUNC(avs_get_frame, 0);
  117. LOAD_AVS_FUNC(avs_get_version, 0);
  118. LOAD_AVS_FUNC(avs_get_video_info, 0);
  119. LOAD_AVS_FUNC(avs_invoke, 0);
  120. LOAD_AVS_FUNC(avs_release_clip, 0);
  121. LOAD_AVS_FUNC(avs_release_value, 0);
  122. LOAD_AVS_FUNC(avs_release_video_frame, 0);
  123. LOAD_AVS_FUNC(avs_take_clip, 0);
  124. #ifdef USING_AVISYNTH
  125. LOAD_AVS_FUNC(avs_bits_per_pixel, 0);
  126. LOAD_AVS_FUNC(avs_get_height_p, 0);
  127. LOAD_AVS_FUNC(avs_get_pitch_p, 0);
  128. LOAD_AVS_FUNC(avs_get_read_ptr_p, 0);
  129. LOAD_AVS_FUNC(avs_get_row_size_p, 0);
  130. LOAD_AVS_FUNC(avs_is_yv24, 0);
  131. LOAD_AVS_FUNC(avs_is_yv16, 0);
  132. LOAD_AVS_FUNC(avs_is_yv411, 0);
  133. LOAD_AVS_FUNC(avs_is_y8, 0);
  134. #endif
  135. #undef LOAD_AVS_FUNC
  136. atexit(avisynth_atexit_handler);
  137. return 0;
  138. fail:
  139. FreeLibrary(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. FreeLibrary(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
  205. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  206. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  207. st->codec->width = avs->vi->width;
  208. st->codec->height = avs->vi->height;
  209. st->time_base = (AVRational) { avs->vi->fps_denominator,
  210. avs->vi->fps_numerator };
  211. st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
  212. avs->vi->fps_denominator };
  213. st->start_time = 0;
  214. st->duration = avs->vi->num_frames;
  215. st->nb_frames = avs->vi->num_frames;
  216. switch (avs->vi->pixel_type) {
  217. #ifdef USING_AVISYNTH
  218. case AVS_CS_YV24:
  219. st->codec->pix_fmt = AV_PIX_FMT_YUV444P;
  220. planar = 1;
  221. break;
  222. case AVS_CS_YV16:
  223. st->codec->pix_fmt = AV_PIX_FMT_YUV422P;
  224. planar = 1;
  225. break;
  226. case AVS_CS_YV411:
  227. st->codec->pix_fmt = AV_PIX_FMT_YUV411P;
  228. planar = 1;
  229. break;
  230. case AVS_CS_Y8:
  231. st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
  232. planar = 2;
  233. break;
  234. #endif
  235. case AVS_CS_BGR24:
  236. st->codec->pix_fmt = AV_PIX_FMT_BGR24;
  237. break;
  238. case AVS_CS_BGR32:
  239. st->codec->pix_fmt = AV_PIX_FMT_RGB32;
  240. break;
  241. case AVS_CS_YUY2:
  242. st->codec->pix_fmt = AV_PIX_FMT_YUYV422;
  243. break;
  244. case AVS_CS_YV12:
  245. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  246. planar = 1;
  247. break;
  248. case AVS_CS_I420: // Is this even used anywhere?
  249. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  250. planar = 1;
  251. break;
  252. default:
  253. av_log(s, AV_LOG_ERROR,
  254. "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  255. avs->error = 1;
  256. return AVERROR_UNKNOWN;
  257. }
  258. switch (planar) {
  259. case 2: // Y8
  260. avs->n_planes = 1;
  261. avs->planes = avs_planes_grey;
  262. break;
  263. case 1: // YUV
  264. avs->n_planes = 3;
  265. avs->planes = avs_planes_yuv;
  266. break;
  267. default:
  268. avs->n_planes = 1;
  269. avs->planes = avs_planes_packed;
  270. }
  271. return 0;
  272. }
  273. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
  274. {
  275. AviSynthContext *avs = s->priv_data;
  276. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  277. st->codec->sample_rate = avs->vi->audio_samples_per_second;
  278. st->codec->channels = avs->vi->nchannels;
  279. st->time_base = (AVRational) { 1,
  280. avs->vi->audio_samples_per_second };
  281. st->duration = avs->vi->num_audio_samples;
  282. switch (avs->vi->sample_type) {
  283. case AVS_SAMPLE_INT8:
  284. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  285. break;
  286. case AVS_SAMPLE_INT16:
  287. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  288. break;
  289. case AVS_SAMPLE_INT24:
  290. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  291. break;
  292. case AVS_SAMPLE_INT32:
  293. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  294. break;
  295. case AVS_SAMPLE_FLOAT:
  296. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  297. break;
  298. default:
  299. av_log(s, AV_LOG_ERROR,
  300. "unknown AviSynth sample type %d\n", avs->vi->sample_type);
  301. avs->error = 1;
  302. return AVERROR_UNKNOWN;
  303. }
  304. return 0;
  305. }
  306. static int avisynth_create_stream(AVFormatContext *s)
  307. {
  308. AviSynthContext *avs = s->priv_data;
  309. AVStream *st;
  310. int ret;
  311. int id = 0;
  312. if (avs_has_video(avs->vi)) {
  313. st = avformat_new_stream(s, NULL);
  314. if (!st)
  315. return AVERROR_UNKNOWN;
  316. st->id = id++;
  317. if (ret = avisynth_create_stream_video(s, st))
  318. return ret;
  319. }
  320. if (avs_has_audio(avs->vi)) {
  321. st = avformat_new_stream(s, NULL);
  322. if (!st)
  323. return AVERROR_UNKNOWN;
  324. st->id = id++;
  325. if (ret = avisynth_create_stream_audio(s, st))
  326. return ret;
  327. }
  328. return 0;
  329. }
  330. static int avisynth_open_file(AVFormatContext *s)
  331. {
  332. AviSynthContext *avs = s->priv_data;
  333. AVS_Value arg, val;
  334. int ret;
  335. #ifdef USING_AVISYNTH
  336. char filename_ansi[MAX_PATH * 4];
  337. wchar_t filename_wc[MAX_PATH * 4];
  338. #endif
  339. if (ret = avisynth_context_create(s))
  340. return ret;
  341. #ifdef USING_AVISYNTH
  342. /* Convert UTF-8 to ANSI code page */
  343. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
  344. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
  345. MAX_PATH * 4, NULL, NULL);
  346. arg = avs_new_value_string(filename_ansi);
  347. #else
  348. arg = avs_new_value_string(s->filename);
  349. #endif
  350. val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
  351. if (avs_is_error(val)) {
  352. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  353. ret = AVERROR_UNKNOWN;
  354. goto fail;
  355. }
  356. if (!avs_is_clip(val)) {
  357. av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
  358. ret = AVERROR_UNKNOWN;
  359. goto fail;
  360. }
  361. avs->clip = avs_library.avs_take_clip(val, avs->env);
  362. avs->vi = avs_library.avs_get_video_info(avs->clip);
  363. #ifdef USING_AVISYNTH
  364. /* FFmpeg only supports AviSynth 2.6 on Windows. Since AvxSynth
  365. * identifies itself as interface version 3 like 2.5.8, this
  366. * needs to be special-cased. */
  367. if (avs_library.avs_get_version(avs->clip) == 3) {
  368. av_log(s, AV_LOG_ERROR,
  369. "AviSynth 2.5.8 not supported. Please upgrade to 2.6.\n");
  370. ret = AVERROR_UNKNOWN;
  371. goto fail;
  372. }
  373. #endif
  374. /* Release the AVS_Value as it will go out of scope. */
  375. avs_library.avs_release_value(val);
  376. if (ret = avisynth_create_stream(s))
  377. goto fail;
  378. return 0;
  379. fail:
  380. avisynth_context_destroy(avs);
  381. return ret;
  382. }
  383. static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
  384. AVPacket *pkt, int *discard)
  385. {
  386. AviSynthContext *avs = s->priv_data;
  387. avs->curr_stream++;
  388. avs->curr_stream %= s->nb_streams;
  389. *st = s->streams[avs->curr_stream];
  390. if ((*st)->discard == AVDISCARD_ALL)
  391. *discard = 1;
  392. else
  393. *discard = 0;
  394. return;
  395. }
  396. /* Copy AviSynth clip data into an AVPacket. */
  397. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
  398. int discard)
  399. {
  400. AviSynthContext *avs = s->priv_data;
  401. AVS_VideoFrame *frame;
  402. unsigned char *dst_p;
  403. const unsigned char *src_p;
  404. int n, i, plane, rowsize, planeheight, pitch, bits;
  405. const char *error;
  406. if (avs->curr_frame >= avs->vi->num_frames)
  407. return AVERROR_EOF;
  408. /* This must happen even if the stream is discarded to prevent desync. */
  409. n = avs->curr_frame++;
  410. if (discard)
  411. return 0;
  412. #ifdef USING_AVISYNTH
  413. /* Define the bpp values for the new AviSynth 2.6 colorspaces.
  414. * Since AvxSynth doesn't have these functions, special-case
  415. * it in order to avoid implicit declaration errors. */
  416. if (avs_library.avs_is_yv24(avs->vi))
  417. bits = 24;
  418. else if (avs_library.avs_is_yv16(avs->vi))
  419. bits = 16;
  420. else if (avs_library.avs_is_yv411(avs->vi))
  421. bits = 12;
  422. else if (avs_library.avs_is_y8(avs->vi))
  423. bits = 8;
  424. else
  425. bits = avs_library.avs_bits_per_pixel(avs->vi);
  426. #else
  427. bits = avs_bits_per_pixel(avs->vi);
  428. #endif
  429. /* Without the cast to int64_t, calculation overflows at about 9k x 9k
  430. * resolution. */
  431. pkt->size = (((int64_t)avs->vi->width *
  432. (int64_t)avs->vi->height) * bits) / 8;
  433. if (!pkt->size)
  434. return AVERROR_UNKNOWN;
  435. if (av_new_packet(pkt, pkt->size) < 0)
  436. return AVERROR(ENOMEM);
  437. pkt->pts = n;
  438. pkt->dts = n;
  439. pkt->duration = 1;
  440. pkt->stream_index = avs->curr_stream;
  441. frame = avs_library.avs_get_frame(avs->clip, n);
  442. error = avs_library.avs_clip_get_error(avs->clip);
  443. if (error) {
  444. av_log(s, AV_LOG_ERROR, "%s\n", error);
  445. avs->error = 1;
  446. av_packet_unref(pkt);
  447. return AVERROR_UNKNOWN;
  448. }
  449. dst_p = pkt->data;
  450. for (i = 0; i < avs->n_planes; i++) {
  451. plane = avs->planes[i];
  452. #ifdef USING_AVISYNTH
  453. src_p = avs_library.avs_get_read_ptr_p(frame, plane);
  454. pitch = avs_library.avs_get_pitch_p(frame, plane);
  455. rowsize = avs_library.avs_get_row_size_p(frame, plane);
  456. planeheight = avs_library.avs_get_height_p(frame, plane);
  457. #else
  458. src_p = avs_get_read_ptr_p(frame, plane);
  459. pitch = avs_get_pitch_p(frame, plane);
  460. rowsize = avs_get_row_size_p(frame, plane);
  461. planeheight = avs_get_height_p(frame, plane);
  462. #endif
  463. /* Flip RGB video. */
  464. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
  465. src_p = src_p + (planeheight - 1) * pitch;
  466. pitch = -pitch;
  467. }
  468. avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
  469. rowsize, planeheight);
  470. dst_p += rowsize * planeheight;
  471. }
  472. avs_library.avs_release_video_frame(frame);
  473. return 0;
  474. }
  475. static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
  476. int discard)
  477. {
  478. AviSynthContext *avs = s->priv_data;
  479. AVRational fps, samplerate;
  480. int samples;
  481. int64_t n;
  482. const char *error;
  483. if (avs->curr_sample >= avs->vi->num_audio_samples)
  484. return AVERROR_EOF;
  485. fps.num = avs->vi->fps_numerator;
  486. fps.den = avs->vi->fps_denominator;
  487. samplerate.num = avs->vi->audio_samples_per_second;
  488. samplerate.den = 1;
  489. if (avs_has_video(avs->vi)) {
  490. if (avs->curr_frame < avs->vi->num_frames)
  491. samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
  492. avs->curr_sample;
  493. else
  494. samples = av_rescale_q(1, samplerate, fps);
  495. } else {
  496. samples = 1000;
  497. }
  498. /* After seeking, audio may catch up with video. */
  499. if (samples <= 0) {
  500. pkt->size = 0;
  501. pkt->data = NULL;
  502. return 0;
  503. }
  504. if (avs->curr_sample + samples > avs->vi->num_audio_samples)
  505. samples = avs->vi->num_audio_samples - avs->curr_sample;
  506. /* This must happen even if the stream is discarded to prevent desync. */
  507. n = avs->curr_sample;
  508. avs->curr_sample += samples;
  509. if (discard)
  510. return 0;
  511. pkt->size = avs_bytes_per_channel_sample(avs->vi) *
  512. samples * avs->vi->nchannels;
  513. if (!pkt->size)
  514. return AVERROR_UNKNOWN;
  515. if (av_new_packet(pkt, pkt->size) < 0)
  516. return AVERROR(ENOMEM);
  517. pkt->pts = n;
  518. pkt->dts = n;
  519. pkt->duration = samples;
  520. pkt->stream_index = avs->curr_stream;
  521. avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
  522. error = avs_library.avs_clip_get_error(avs->clip);
  523. if (error) {
  524. av_log(s, AV_LOG_ERROR, "%s\n", error);
  525. avs->error = 1;
  526. av_packet_unref(pkt);
  527. return AVERROR_UNKNOWN;
  528. }
  529. return 0;
  530. }
  531. static av_cold int avisynth_read_header(AVFormatContext *s)
  532. {
  533. int ret;
  534. // Calling library must implement a lock for thread-safe opens.
  535. if (ret = avpriv_lock_avformat())
  536. return ret;
  537. if (ret = avisynth_open_file(s)) {
  538. avpriv_unlock_avformat();
  539. return ret;
  540. }
  541. avpriv_unlock_avformat();
  542. return 0;
  543. }
  544. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  545. {
  546. AviSynthContext *avs = s->priv_data;
  547. AVStream *st;
  548. int discard = 0;
  549. int ret;
  550. if (avs->error)
  551. return AVERROR_UNKNOWN;
  552. /* If either stream reaches EOF, try to read the other one before
  553. * giving up. */
  554. avisynth_next_stream(s, &st, pkt, &discard);
  555. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  556. ret = avisynth_read_packet_video(s, pkt, discard);
  557. if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
  558. avisynth_next_stream(s, &st, pkt, &discard);
  559. return avisynth_read_packet_audio(s, pkt, discard);
  560. }
  561. } else {
  562. ret = avisynth_read_packet_audio(s, pkt, discard);
  563. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  564. avisynth_next_stream(s, &st, pkt, &discard);
  565. return avisynth_read_packet_video(s, pkt, discard);
  566. }
  567. }
  568. return ret;
  569. }
  570. static av_cold int avisynth_read_close(AVFormatContext *s)
  571. {
  572. if (avpriv_lock_avformat())
  573. return AVERROR_UNKNOWN;
  574. avisynth_context_destroy(s->priv_data);
  575. avpriv_unlock_avformat();
  576. return 0;
  577. }
  578. static int avisynth_read_seek(AVFormatContext *s, int stream_index,
  579. int64_t timestamp, int flags)
  580. {
  581. AviSynthContext *avs = s->priv_data;
  582. AVStream *st;
  583. AVRational fps, samplerate;
  584. if (avs->error)
  585. return AVERROR_UNKNOWN;
  586. fps = (AVRational) { avs->vi->fps_numerator,
  587. avs->vi->fps_denominator };
  588. samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
  589. st = s->streams[stream_index];
  590. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  591. /* AviSynth frame counts are signed int. */
  592. if ((timestamp >= avs->vi->num_frames) ||
  593. (timestamp > INT_MAX) ||
  594. (timestamp < 0))
  595. return AVERROR_EOF;
  596. avs->curr_frame = timestamp;
  597. if (avs_has_audio(avs->vi))
  598. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  599. } else {
  600. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  601. return AVERROR_EOF;
  602. /* Force frame granularity for seeking. */
  603. if (avs_has_video(avs->vi)) {
  604. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  605. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  606. } else {
  607. avs->curr_sample = timestamp;
  608. }
  609. }
  610. return 0;
  611. }
  612. AVInputFormat ff_avisynth_demuxer = {
  613. .name = "avisynth",
  614. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  615. .priv_data_size = sizeof(AviSynthContext),
  616. .read_header = avisynth_read_header,
  617. .read_packet = avisynth_read_packet,
  618. .read_close = avisynth_read_close,
  619. .read_seek = avisynth_read_seek,
  620. .extensions = "avs",
  621. };