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.

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