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.

717 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->time_base = (AVRational) { avs->vi->fps_denominator,
  208. avs->vi->fps_numerator };
  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. switch (avs->vi->pixel_type) {
  215. #ifdef USING_AVISYNTH
  216. case AVS_CS_YV24:
  217. st->codec->pix_fmt = AV_PIX_FMT_YUV444P;
  218. planar = 1;
  219. break;
  220. case AVS_CS_YV16:
  221. st->codec->pix_fmt = AV_PIX_FMT_YUV422P;
  222. planar = 1;
  223. break;
  224. case AVS_CS_YV411:
  225. st->codec->pix_fmt = AV_PIX_FMT_YUV411P;
  226. planar = 1;
  227. break;
  228. case AVS_CS_Y8:
  229. st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
  230. planar = 2;
  231. break;
  232. #endif
  233. case AVS_CS_BGR24:
  234. st->codec->pix_fmt = AV_PIX_FMT_BGR24;
  235. break;
  236. case AVS_CS_BGR32:
  237. st->codec->pix_fmt = AV_PIX_FMT_RGB32;
  238. break;
  239. case AVS_CS_YUY2:
  240. st->codec->pix_fmt = AV_PIX_FMT_YUYV422;
  241. break;
  242. case AVS_CS_YV12:
  243. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  244. planar = 1;
  245. break;
  246. case AVS_CS_I420: // Is this even used anywhere?
  247. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  248. planar = 1;
  249. break;
  250. default:
  251. av_log(s, AV_LOG_ERROR,
  252. "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  253. avs->error = 1;
  254. return AVERROR_UNKNOWN;
  255. }
  256. switch (planar) {
  257. case 2: // Y8
  258. avs->n_planes = 1;
  259. avs->planes = avs_planes_grey;
  260. break;
  261. case 1: // YUV
  262. avs->n_planes = 3;
  263. avs->planes = avs_planes_yuv;
  264. break;
  265. default:
  266. avs->n_planes = 1;
  267. avs->planes = avs_planes_packed;
  268. }
  269. return 0;
  270. }
  271. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
  272. {
  273. AviSynthContext *avs = s->priv_data;
  274. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  275. st->codec->sample_rate = avs->vi->audio_samples_per_second;
  276. st->codec->channels = avs->vi->nchannels;
  277. st->time_base = (AVRational) { 1,
  278. avs->vi->audio_samples_per_second };
  279. st->duration = avs->vi->num_audio_samples;
  280. switch (avs->vi->sample_type) {
  281. case AVS_SAMPLE_INT8:
  282. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  283. break;
  284. case AVS_SAMPLE_INT16:
  285. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  286. break;
  287. case AVS_SAMPLE_INT24:
  288. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  289. break;
  290. case AVS_SAMPLE_INT32:
  291. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  292. break;
  293. case AVS_SAMPLE_FLOAT:
  294. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  295. break;
  296. default:
  297. av_log(s, AV_LOG_ERROR,
  298. "unknown AviSynth sample type %d\n", avs->vi->sample_type);
  299. avs->error = 1;
  300. return AVERROR_UNKNOWN;
  301. }
  302. return 0;
  303. }
  304. static int avisynth_create_stream(AVFormatContext *s)
  305. {
  306. AviSynthContext *avs = s->priv_data;
  307. AVStream *st;
  308. int ret;
  309. int id = 0;
  310. if (avs_has_video(avs->vi)) {
  311. st = avformat_new_stream(s, NULL);
  312. if (!st)
  313. return AVERROR_UNKNOWN;
  314. st->id = id++;
  315. if (ret = avisynth_create_stream_video(s, st))
  316. return ret;
  317. }
  318. if (avs_has_audio(avs->vi)) {
  319. st = avformat_new_stream(s, NULL);
  320. if (!st)
  321. return AVERROR_UNKNOWN;
  322. st->id = id++;
  323. if (ret = avisynth_create_stream_audio(s, st))
  324. return ret;
  325. }
  326. return 0;
  327. }
  328. static int avisynth_open_file(AVFormatContext *s)
  329. {
  330. AviSynthContext *avs = s->priv_data;
  331. AVS_Value arg, val;
  332. int ret;
  333. #ifdef USING_AVISYNTH
  334. char filename_ansi[MAX_PATH * 4];
  335. wchar_t filename_wc[MAX_PATH * 4];
  336. #endif
  337. if (ret = avisynth_context_create(s))
  338. return ret;
  339. #ifdef USING_AVISYNTH
  340. /* Convert UTF-8 to ANSI code page */
  341. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
  342. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
  343. MAX_PATH * 4, NULL, NULL);
  344. arg = avs_new_value_string(filename_ansi);
  345. #else
  346. arg = avs_new_value_string(s->filename);
  347. #endif
  348. val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
  349. if (avs_is_error(val)) {
  350. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  351. ret = AVERROR_UNKNOWN;
  352. goto fail;
  353. }
  354. if (!avs_is_clip(val)) {
  355. av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
  356. ret = AVERROR_UNKNOWN;
  357. goto fail;
  358. }
  359. avs->clip = avs_library.avs_take_clip(val, avs->env);
  360. avs->vi = avs_library.avs_get_video_info(avs->clip);
  361. #ifdef USING_AVISYNTH
  362. /* On Windows, FFmpeg supports AviSynth interface version 6 or higher.
  363. * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
  364. * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
  365. * as interface version 3 like 2.5.8, this needs to be special-cased. */
  366. if (avs_library.avs_get_version(avs->clip) < 6) {
  367. av_log(s, AV_LOG_ERROR,
  368. "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
  369. ret = AVERROR_UNKNOWN;
  370. goto fail;
  371. }
  372. #endif
  373. /* Release the AVS_Value as it will go out of scope. */
  374. avs_library.avs_release_value(val);
  375. if (ret = avisynth_create_stream(s))
  376. goto fail;
  377. return 0;
  378. fail:
  379. avisynth_context_destroy(avs);
  380. return ret;
  381. }
  382. static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
  383. AVPacket *pkt, int *discard)
  384. {
  385. AviSynthContext *avs = s->priv_data;
  386. avs->curr_stream++;
  387. avs->curr_stream %= s->nb_streams;
  388. *st = s->streams[avs->curr_stream];
  389. if ((*st)->discard == AVDISCARD_ALL)
  390. *discard = 1;
  391. else
  392. *discard = 0;
  393. return;
  394. }
  395. /* Copy AviSynth clip data into an AVPacket. */
  396. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
  397. int discard)
  398. {
  399. AviSynthContext *avs = s->priv_data;
  400. AVS_VideoFrame *frame;
  401. unsigned char *dst_p;
  402. const unsigned char *src_p;
  403. int n, i, plane, rowsize, planeheight, pitch, bits;
  404. const char *error;
  405. if (avs->curr_frame >= avs->vi->num_frames)
  406. return AVERROR_EOF;
  407. /* This must happen even if the stream is discarded to prevent desync. */
  408. n = avs->curr_frame++;
  409. if (discard)
  410. return 0;
  411. #ifdef USING_AVISYNTH
  412. /* Define the bpp values for the new AviSynth 2.6 colorspaces.
  413. * Since AvxSynth doesn't have these functions, special-case
  414. * it in order to avoid implicit declaration errors. */
  415. if (avs_library.avs_is_yv24(avs->vi))
  416. bits = 24;
  417. else if (avs_library.avs_is_yv16(avs->vi))
  418. bits = 16;
  419. else if (avs_library.avs_is_yv411(avs->vi))
  420. bits = 12;
  421. else if (avs_library.avs_is_y8(avs->vi))
  422. bits = 8;
  423. else
  424. bits = avs_library.avs_bits_per_pixel(avs->vi);
  425. #else
  426. bits = avs_bits_per_pixel(avs->vi);
  427. #endif
  428. /* Without the cast to int64_t, calculation overflows at about 9k x 9k
  429. * resolution. */
  430. pkt->size = (((int64_t)avs->vi->width *
  431. (int64_t)avs->vi->height) * bits) / 8;
  432. if (!pkt->size)
  433. return AVERROR_UNKNOWN;
  434. if (av_new_packet(pkt, pkt->size) < 0)
  435. return AVERROR(ENOMEM);
  436. pkt->pts = n;
  437. pkt->dts = n;
  438. pkt->duration = 1;
  439. pkt->stream_index = avs->curr_stream;
  440. frame = avs_library.avs_get_frame(avs->clip, n);
  441. error = avs_library.avs_clip_get_error(avs->clip);
  442. if (error) {
  443. av_log(s, AV_LOG_ERROR, "%s\n", error);
  444. avs->error = 1;
  445. av_packet_unref(pkt);
  446. return AVERROR_UNKNOWN;
  447. }
  448. dst_p = pkt->data;
  449. for (i = 0; i < avs->n_planes; i++) {
  450. plane = avs->planes[i];
  451. #ifdef USING_AVISYNTH
  452. src_p = avs_library.avs_get_read_ptr_p(frame, plane);
  453. pitch = avs_library.avs_get_pitch_p(frame, plane);
  454. rowsize = avs_library.avs_get_row_size_p(frame, plane);
  455. planeheight = avs_library.avs_get_height_p(frame, plane);
  456. #else
  457. src_p = avs_get_read_ptr_p(frame, plane);
  458. pitch = avs_get_pitch_p(frame, plane);
  459. rowsize = avs_get_row_size_p(frame, plane);
  460. planeheight = avs_get_height_p(frame, plane);
  461. #endif
  462. /* Flip RGB video. */
  463. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
  464. src_p = src_p + (planeheight - 1) * pitch;
  465. pitch = -pitch;
  466. }
  467. avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
  468. rowsize, planeheight);
  469. dst_p += rowsize * planeheight;
  470. }
  471. avs_library.avs_release_video_frame(frame);
  472. return 0;
  473. }
  474. static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
  475. int discard)
  476. {
  477. AviSynthContext *avs = s->priv_data;
  478. AVRational fps, samplerate;
  479. int samples;
  480. int64_t n;
  481. const char *error;
  482. if (avs->curr_sample >= avs->vi->num_audio_samples)
  483. return AVERROR_EOF;
  484. fps.num = avs->vi->fps_numerator;
  485. fps.den = avs->vi->fps_denominator;
  486. samplerate.num = avs->vi->audio_samples_per_second;
  487. samplerate.den = 1;
  488. if (avs_has_video(avs->vi)) {
  489. if (avs->curr_frame < avs->vi->num_frames)
  490. samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
  491. avs->curr_sample;
  492. else
  493. samples = av_rescale_q(1, samplerate, fps);
  494. } else {
  495. samples = 1000;
  496. }
  497. /* After seeking, audio may catch up with video. */
  498. if (samples <= 0) {
  499. pkt->size = 0;
  500. pkt->data = NULL;
  501. return 0;
  502. }
  503. if (avs->curr_sample + samples > avs->vi->num_audio_samples)
  504. samples = avs->vi->num_audio_samples - avs->curr_sample;
  505. /* This must happen even if the stream is discarded to prevent desync. */
  506. n = avs->curr_sample;
  507. avs->curr_sample += samples;
  508. if (discard)
  509. return 0;
  510. pkt->size = avs_bytes_per_channel_sample(avs->vi) *
  511. samples * avs->vi->nchannels;
  512. if (!pkt->size)
  513. return AVERROR_UNKNOWN;
  514. if (av_new_packet(pkt, pkt->size) < 0)
  515. return AVERROR(ENOMEM);
  516. pkt->pts = n;
  517. pkt->dts = n;
  518. pkt->duration = samples;
  519. pkt->stream_index = avs->curr_stream;
  520. avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
  521. error = avs_library.avs_clip_get_error(avs->clip);
  522. if (error) {
  523. av_log(s, AV_LOG_ERROR, "%s\n", error);
  524. avs->error = 1;
  525. av_packet_unref(pkt);
  526. return AVERROR_UNKNOWN;
  527. }
  528. return 0;
  529. }
  530. static av_cold int avisynth_read_header(AVFormatContext *s)
  531. {
  532. int ret;
  533. // Calling library must implement a lock for thread-safe opens.
  534. if (ret = avpriv_lock_avformat())
  535. return ret;
  536. if (ret = avisynth_open_file(s)) {
  537. avpriv_unlock_avformat();
  538. return ret;
  539. }
  540. avpriv_unlock_avformat();
  541. return 0;
  542. }
  543. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  544. {
  545. AviSynthContext *avs = s->priv_data;
  546. AVStream *st;
  547. int discard = 0;
  548. int ret;
  549. if (avs->error)
  550. return AVERROR_UNKNOWN;
  551. /* If either stream reaches EOF, try to read the other one before
  552. * giving up. */
  553. avisynth_next_stream(s, &st, pkt, &discard);
  554. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  555. ret = avisynth_read_packet_video(s, pkt, discard);
  556. if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
  557. avisynth_next_stream(s, &st, pkt, &discard);
  558. return avisynth_read_packet_audio(s, pkt, discard);
  559. }
  560. } else {
  561. ret = avisynth_read_packet_audio(s, pkt, discard);
  562. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  563. avisynth_next_stream(s, &st, pkt, &discard);
  564. return avisynth_read_packet_video(s, pkt, discard);
  565. }
  566. }
  567. return ret;
  568. }
  569. static av_cold int avisynth_read_close(AVFormatContext *s)
  570. {
  571. if (avpriv_lock_avformat())
  572. return AVERROR_UNKNOWN;
  573. avisynth_context_destroy(s->priv_data);
  574. avpriv_unlock_avformat();
  575. return 0;
  576. }
  577. static int avisynth_read_seek(AVFormatContext *s, int stream_index,
  578. int64_t timestamp, int flags)
  579. {
  580. AviSynthContext *avs = s->priv_data;
  581. AVStream *st;
  582. AVRational fps, samplerate;
  583. if (avs->error)
  584. return AVERROR_UNKNOWN;
  585. fps = (AVRational) { avs->vi->fps_numerator,
  586. avs->vi->fps_denominator };
  587. samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
  588. st = s->streams[stream_index];
  589. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  590. /* AviSynth frame counts are signed int. */
  591. if ((timestamp >= avs->vi->num_frames) ||
  592. (timestamp > INT_MAX) ||
  593. (timestamp < 0))
  594. return AVERROR_EOF;
  595. avs->curr_frame = timestamp;
  596. if (avs_has_audio(avs->vi))
  597. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  598. } else {
  599. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  600. return AVERROR_EOF;
  601. /* Force frame granularity for seeking. */
  602. if (avs_has_video(avs->vi)) {
  603. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  604. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  605. } else {
  606. avs->curr_sample = timestamp;
  607. }
  608. }
  609. return 0;
  610. }
  611. AVInputFormat ff_avisynth_demuxer = {
  612. .name = "avisynth",
  613. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  614. .priv_data_size = sizeof(AviSynthContext),
  615. .read_header = avisynth_read_header,
  616. .read_packet = avisynth_read_packet,
  617. .read_close = avisynth_read_close,
  618. .read_seek = avisynth_read_seek,
  619. .extensions = "avs",
  620. };