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 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/internal.h"
  22. #include "libavcodec/internal.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "config.h"
  26. /* Enable function pointer definitions for runtime loading. */
  27. #define AVSC_NO_DECLSPEC
  28. /* Platform-specific directives for AviSynth vs AvxSynth. */
  29. #ifdef _WIN32
  30. #include <windows.h>
  31. #undef EXTERN_C
  32. #include <avisynth/avisynth_c.h>
  33. #define AVISYNTH_LIB "avisynth"
  34. #define USING_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. #ifdef USING_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_yv24);
  68. AVSC_DECLARE_FUNC(avs_is_yv16);
  69. AVSC_DECLARE_FUNC(avs_is_yv411);
  70. AVSC_DECLARE_FUNC(avs_is_y8);
  71. #endif
  72. #undef AVSC_DECLARE_FUNC
  73. } AviSynthLibrary;
  74. typedef struct AviSynthContext {
  75. AVS_ScriptEnvironment *env;
  76. AVS_Clip *clip;
  77. const AVS_VideoInfo *vi;
  78. /* avisynth_read_packet_video() iterates over this. */
  79. int n_planes;
  80. const int *planes;
  81. int curr_stream;
  82. int curr_frame;
  83. int64_t curr_sample;
  84. int error;
  85. /* Linked list pointers. */
  86. struct AviSynthContext *next;
  87. } AviSynthContext;
  88. static const int avs_planes_packed[1] = { 0 };
  89. static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
  90. static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
  91. AVS_PLANAR_V };
  92. /* A conflict between C++ global objects, atexit, and dynamic loading requires
  93. * us to register our own atexit handler to prevent double freeing. */
  94. static AviSynthLibrary avs_library;
  95. static int avs_atexit_called = 0;
  96. /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
  97. static AviSynthContext *avs_ctx_list = NULL;
  98. static av_cold void avisynth_atexit_handler(void);
  99. static av_cold int avisynth_load_library(void)
  100. {
  101. avs_library.library = LoadLibrary(AVISYNTH_LIB);
  102. if (!avs_library.library)
  103. return AVERROR_UNKNOWN;
  104. #define LOAD_AVS_FUNC(name, continue_on_fail) \
  105. avs_library.name = \
  106. (void *)GetProcAddress(avs_library.library, #name); \
  107. if (!continue_on_fail && !avs_library.name) \
  108. goto fail;
  109. LOAD_AVS_FUNC(avs_bit_blt, 0);
  110. LOAD_AVS_FUNC(avs_clip_get_error, 0);
  111. LOAD_AVS_FUNC(avs_create_script_environment, 0);
  112. LOAD_AVS_FUNC(avs_delete_script_environment, 0);
  113. LOAD_AVS_FUNC(avs_get_audio, 0);
  114. LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
  115. LOAD_AVS_FUNC(avs_get_frame, 0);
  116. LOAD_AVS_FUNC(avs_get_version, 0);
  117. LOAD_AVS_FUNC(avs_get_video_info, 0);
  118. LOAD_AVS_FUNC(avs_invoke, 0);
  119. LOAD_AVS_FUNC(avs_release_clip, 0);
  120. LOAD_AVS_FUNC(avs_release_value, 0);
  121. LOAD_AVS_FUNC(avs_release_video_frame, 0);
  122. LOAD_AVS_FUNC(avs_take_clip, 0);
  123. #ifdef USING_AVISYNTH
  124. LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
  125. LOAD_AVS_FUNC(avs_get_height_p, 1);
  126. LOAD_AVS_FUNC(avs_get_pitch_p, 1);
  127. LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
  128. LOAD_AVS_FUNC(avs_get_row_size_p, 1);
  129. LOAD_AVS_FUNC(avs_is_yv24, 1);
  130. LOAD_AVS_FUNC(avs_is_yv16, 1);
  131. LOAD_AVS_FUNC(avs_is_yv411, 1);
  132. LOAD_AVS_FUNC(avs_is_y8, 1);
  133. #endif
  134. #undef LOAD_AVS_FUNC
  135. atexit(avisynth_atexit_handler);
  136. return 0;
  137. fail:
  138. FreeLibrary(avs_library.library);
  139. return AVERROR_UNKNOWN;
  140. }
  141. /* Note that avisynth_context_create and avisynth_context_destroy
  142. * do not allocate or free the actual context! That is taken care of
  143. * by libavformat. */
  144. static av_cold int avisynth_context_create(AVFormatContext *s)
  145. {
  146. AviSynthContext *avs = s->priv_data;
  147. int ret;
  148. if (!avs_library.library)
  149. if (ret = avisynth_load_library())
  150. return ret;
  151. avs->env = avs_library.avs_create_script_environment(3);
  152. if (avs_library.avs_get_error) {
  153. const char *error = avs_library.avs_get_error(avs->env);
  154. if (error) {
  155. av_log(s, AV_LOG_ERROR, "%s\n", error);
  156. return AVERROR_UNKNOWN;
  157. }
  158. }
  159. if (!avs_ctx_list) {
  160. avs_ctx_list = avs;
  161. } else {
  162. avs->next = avs_ctx_list;
  163. avs_ctx_list = avs;
  164. }
  165. return 0;
  166. }
  167. static av_cold void avisynth_context_destroy(AviSynthContext *avs)
  168. {
  169. if (avs_atexit_called)
  170. return;
  171. if (avs == avs_ctx_list) {
  172. avs_ctx_list = avs->next;
  173. } else {
  174. AviSynthContext *prev = avs_ctx_list;
  175. while (prev->next != avs)
  176. prev = prev->next;
  177. prev->next = avs->next;
  178. }
  179. if (avs->clip) {
  180. avs_library.avs_release_clip(avs->clip);
  181. avs->clip = NULL;
  182. }
  183. if (avs->env) {
  184. avs_library.avs_delete_script_environment(avs->env);
  185. avs->env = NULL;
  186. }
  187. }
  188. static av_cold void avisynth_atexit_handler(void)
  189. {
  190. AviSynthContext *avs = avs_ctx_list;
  191. while (avs) {
  192. AviSynthContext *next = avs->next;
  193. avisynth_context_destroy(avs);
  194. avs = next;
  195. }
  196. FreeLibrary(avs_library.library);
  197. avs_atexit_called = 1;
  198. }
  199. /* Create AVStream from audio and video data. */
  200. static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
  201. {
  202. AviSynthContext *avs = s->priv_data;
  203. int planar = 0; // 0: packed, 1: YUV, 2: Y8
  204. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  205. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  206. st->codecpar->width = avs->vi->width;
  207. st->codecpar->height = avs->vi->height;
  208. st->time_base = (AVRational) { avs->vi->fps_denominator,
  209. avs->vi->fps_numerator };
  210. st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
  211. avs->vi->fps_denominator };
  212. st->start_time = 0;
  213. st->duration = avs->vi->num_frames;
  214. st->nb_frames = avs->vi->num_frames;
  215. switch (avs->vi->pixel_type) {
  216. #ifdef USING_AVISYNTH
  217. case AVS_CS_YV24:
  218. st->codecpar->format = AV_PIX_FMT_YUV444P;
  219. planar = 1;
  220. break;
  221. case AVS_CS_YV16:
  222. st->codecpar->format = AV_PIX_FMT_YUV422P;
  223. planar = 1;
  224. break;
  225. case AVS_CS_YV411:
  226. st->codecpar->format = AV_PIX_FMT_YUV411P;
  227. planar = 1;
  228. break;
  229. case AVS_CS_Y8:
  230. st->codecpar->format = AV_PIX_FMT_GRAY8;
  231. planar = 2;
  232. break;
  233. #endif
  234. case AVS_CS_BGR24:
  235. st->codecpar->format = AV_PIX_FMT_BGR24;
  236. break;
  237. case AVS_CS_BGR32:
  238. st->codecpar->format = AV_PIX_FMT_RGB32;
  239. break;
  240. case AVS_CS_YUY2:
  241. st->codecpar->format = AV_PIX_FMT_YUYV422;
  242. break;
  243. case AVS_CS_YV12:
  244. st->codecpar->format = AV_PIX_FMT_YUV420P;
  245. planar = 1;
  246. break;
  247. case AVS_CS_I420: // Is this even used anywhere?
  248. st->codecpar->format = AV_PIX_FMT_YUV420P;
  249. planar = 1;
  250. break;
  251. default:
  252. av_log(s, AV_LOG_ERROR,
  253. "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  254. avs->error = 1;
  255. return AVERROR_UNKNOWN;
  256. }
  257. switch (planar) {
  258. case 2: // Y8
  259. avs->n_planes = 1;
  260. avs->planes = avs_planes_grey;
  261. break;
  262. case 1: // YUV
  263. avs->n_planes = 3;
  264. avs->planes = avs_planes_yuv;
  265. break;
  266. default:
  267. avs->n_planes = 1;
  268. avs->planes = avs_planes_packed;
  269. }
  270. return 0;
  271. }
  272. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
  273. {
  274. AviSynthContext *avs = s->priv_data;
  275. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  276. st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
  277. st->codecpar->channels = avs->vi->nchannels;
  278. st->time_base = (AVRational) { 1,
  279. avs->vi->audio_samples_per_second };
  280. st->duration = avs->vi->num_audio_samples;
  281. switch (avs->vi->sample_type) {
  282. case AVS_SAMPLE_INT8:
  283. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  284. break;
  285. case AVS_SAMPLE_INT16:
  286. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  287. break;
  288. case AVS_SAMPLE_INT24:
  289. st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
  290. break;
  291. case AVS_SAMPLE_INT32:
  292. st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
  293. break;
  294. case AVS_SAMPLE_FLOAT:
  295. st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
  296. break;
  297. default:
  298. av_log(s, AV_LOG_ERROR,
  299. "unknown AviSynth sample type %d\n", avs->vi->sample_type);
  300. avs->error = 1;
  301. return AVERROR_UNKNOWN;
  302. }
  303. return 0;
  304. }
  305. static int avisynth_create_stream(AVFormatContext *s)
  306. {
  307. AviSynthContext *avs = s->priv_data;
  308. AVStream *st;
  309. int ret;
  310. int id = 0;
  311. if (avs_has_video(avs->vi)) {
  312. st = avformat_new_stream(s, NULL);
  313. if (!st)
  314. return AVERROR_UNKNOWN;
  315. st->id = id++;
  316. if (ret = avisynth_create_stream_video(s, st))
  317. return ret;
  318. }
  319. if (avs_has_audio(avs->vi)) {
  320. st = avformat_new_stream(s, NULL);
  321. if (!st)
  322. return AVERROR_UNKNOWN;
  323. st->id = id++;
  324. if (ret = avisynth_create_stream_audio(s, st))
  325. return ret;
  326. }
  327. return 0;
  328. }
  329. static int avisynth_open_file(AVFormatContext *s)
  330. {
  331. AviSynthContext *avs = s->priv_data;
  332. AVS_Value arg, val;
  333. int ret;
  334. #ifdef USING_AVISYNTH
  335. char filename_ansi[MAX_PATH * 4];
  336. wchar_t filename_wc[MAX_PATH * 4];
  337. #endif
  338. if (ret = avisynth_context_create(s))
  339. return ret;
  340. #ifdef USING_AVISYNTH
  341. /* Convert UTF-8 to ANSI code page */
  342. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
  343. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
  344. MAX_PATH * 4, NULL, NULL);
  345. arg = avs_new_value_string(filename_ansi);
  346. #else
  347. arg = avs_new_value_string(s->filename);
  348. #endif
  349. val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
  350. if (avs_is_error(val)) {
  351. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  352. ret = AVERROR_UNKNOWN;
  353. goto fail;
  354. }
  355. if (!avs_is_clip(val)) {
  356. av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
  357. ret = AVERROR_UNKNOWN;
  358. goto fail;
  359. }
  360. avs->clip = avs_library.avs_take_clip(val, avs->env);
  361. avs->vi = avs_library.avs_get_video_info(avs->clip);
  362. #ifdef USING_AVISYNTH
  363. /* On Windows, libav supports AviSynth interface version 6 or higher.
  364. * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
  365. * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
  366. * as interface version 3 like 2.5.8, this needs to be special-cased. */
  367. if (avs_library.avs_get_version(avs->clip) < 6) {
  368. av_log(s, AV_LOG_ERROR,
  369. "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\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->codecpar->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->codecpar->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. };