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.

569 lines
13KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include "libavutil/log.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/bprint.h"
  24. #include "avcodec.h"
  25. #include "bsf.h"
  26. struct AVBSFInternal {
  27. AVPacket *buffer_pkt;
  28. int eof;
  29. };
  30. void av_bsf_free(AVBSFContext **pctx)
  31. {
  32. AVBSFContext *ctx;
  33. if (!pctx || !*pctx)
  34. return;
  35. ctx = *pctx;
  36. if (ctx->filter->close)
  37. ctx->filter->close(ctx);
  38. if (ctx->filter->priv_class && ctx->priv_data)
  39. av_opt_free(ctx->priv_data);
  40. if (ctx->internal)
  41. av_packet_free(&ctx->internal->buffer_pkt);
  42. av_freep(&ctx->internal);
  43. av_freep(&ctx->priv_data);
  44. avcodec_parameters_free(&ctx->par_in);
  45. avcodec_parameters_free(&ctx->par_out);
  46. av_freep(pctx);
  47. }
  48. static void *bsf_child_next(void *obj, void *prev)
  49. {
  50. AVBSFContext *ctx = obj;
  51. if (!prev && ctx->filter->priv_class)
  52. return ctx->priv_data;
  53. return NULL;
  54. }
  55. static const AVClass bsf_class = {
  56. .class_name = "AVBSFContext",
  57. .item_name = av_default_item_name,
  58. .version = LIBAVUTIL_VERSION_INT,
  59. .child_next = bsf_child_next,
  60. .child_class_next = ff_bsf_child_class_next,
  61. };
  62. const AVClass *av_bsf_get_class(void)
  63. {
  64. return &bsf_class;
  65. }
  66. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  67. {
  68. AVBSFContext *ctx;
  69. AVBSFInternal *bsfi;
  70. int ret;
  71. ctx = av_mallocz(sizeof(*ctx));
  72. if (!ctx)
  73. return AVERROR(ENOMEM);
  74. ctx->av_class = &bsf_class;
  75. ctx->filter = filter;
  76. ctx->par_in = avcodec_parameters_alloc();
  77. ctx->par_out = avcodec_parameters_alloc();
  78. if (!ctx->par_in || !ctx->par_out) {
  79. ret = AVERROR(ENOMEM);
  80. goto fail;
  81. }
  82. bsfi = av_mallocz(sizeof(*bsfi));
  83. if (!bsfi) {
  84. ret = AVERROR(ENOMEM);
  85. goto fail;
  86. }
  87. ctx->internal = bsfi;
  88. bsfi->buffer_pkt = av_packet_alloc();
  89. if (!bsfi->buffer_pkt) {
  90. ret = AVERROR(ENOMEM);
  91. goto fail;
  92. }
  93. /* allocate priv data and init private options */
  94. if (filter->priv_data_size) {
  95. ctx->priv_data = av_mallocz(filter->priv_data_size);
  96. if (!ctx->priv_data) {
  97. ret = AVERROR(ENOMEM);
  98. goto fail;
  99. }
  100. if (filter->priv_class) {
  101. *(const AVClass **)ctx->priv_data = filter->priv_class;
  102. av_opt_set_defaults(ctx->priv_data);
  103. }
  104. }
  105. *pctx = ctx;
  106. return 0;
  107. fail:
  108. av_bsf_free(&ctx);
  109. return ret;
  110. }
  111. int av_bsf_init(AVBSFContext *ctx)
  112. {
  113. int ret, i;
  114. /* check that the codec is supported */
  115. if (ctx->filter->codec_ids) {
  116. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  117. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  118. break;
  119. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  120. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  121. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  122. "bitstream filter '%s'. Supported codecs are: ",
  123. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  124. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  125. desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
  126. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  127. desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
  128. }
  129. av_log(ctx, AV_LOG_ERROR, "\n");
  130. return AVERROR(EINVAL);
  131. }
  132. }
  133. /* initialize output parameters to be the same as input
  134. * init below might overwrite that */
  135. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  136. if (ret < 0)
  137. return ret;
  138. ctx->time_base_out = ctx->time_base_in;
  139. if (ctx->filter->init) {
  140. ret = ctx->filter->init(ctx);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. void av_bsf_flush(AVBSFContext *ctx)
  147. {
  148. AVBSFInternal *bsfi = ctx->internal;
  149. bsfi->eof = 0;
  150. av_packet_unref(bsfi->buffer_pkt);
  151. if (ctx->filter->flush)
  152. ctx->filter->flush(ctx);
  153. }
  154. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  155. {
  156. AVBSFInternal *bsfi = ctx->internal;
  157. int ret;
  158. if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
  159. bsfi->eof = 1;
  160. return 0;
  161. }
  162. if (bsfi->eof) {
  163. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  164. return AVERROR(EINVAL);
  165. }
  166. if (bsfi->buffer_pkt->data ||
  167. bsfi->buffer_pkt->side_data_elems)
  168. return AVERROR(EAGAIN);
  169. ret = av_packet_make_refcounted(pkt);
  170. if (ret < 0)
  171. return ret;
  172. av_packet_move_ref(bsfi->buffer_pkt, pkt);
  173. return 0;
  174. }
  175. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  176. {
  177. return ctx->filter->filter(ctx, pkt);
  178. }
  179. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  180. {
  181. AVBSFInternal *bsfi = ctx->internal;
  182. AVPacket *tmp_pkt;
  183. if (bsfi->eof)
  184. return AVERROR_EOF;
  185. if (!bsfi->buffer_pkt->data &&
  186. !bsfi->buffer_pkt->side_data_elems)
  187. return AVERROR(EAGAIN);
  188. tmp_pkt = av_packet_alloc();
  189. if (!tmp_pkt)
  190. return AVERROR(ENOMEM);
  191. *pkt = bsfi->buffer_pkt;
  192. bsfi->buffer_pkt = tmp_pkt;
  193. return 0;
  194. }
  195. int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
  196. {
  197. AVBSFInternal *bsfi = ctx->internal;
  198. if (bsfi->eof)
  199. return AVERROR_EOF;
  200. if (!bsfi->buffer_pkt->data &&
  201. !bsfi->buffer_pkt->side_data_elems)
  202. return AVERROR(EAGAIN);
  203. av_packet_move_ref(pkt, bsfi->buffer_pkt);
  204. return 0;
  205. }
  206. typedef struct BSFListContext {
  207. const AVClass *class;
  208. AVBSFContext **bsfs;
  209. int nb_bsfs;
  210. unsigned idx; // index of currently processed BSF
  211. unsigned flushed_idx; // index of BSF being flushed
  212. char * item_name;
  213. } BSFListContext;
  214. static int bsf_list_init(AVBSFContext *bsf)
  215. {
  216. BSFListContext *lst = bsf->priv_data;
  217. int ret, i;
  218. const AVCodecParameters *cod_par = bsf->par_in;
  219. AVRational tb = bsf->time_base_in;
  220. for (i = 0; i < lst->nb_bsfs; ++i) {
  221. ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
  222. if (ret < 0)
  223. goto fail;
  224. lst->bsfs[i]->time_base_in = tb;
  225. ret = av_bsf_init(lst->bsfs[i]);
  226. if (ret < 0)
  227. goto fail;
  228. cod_par = lst->bsfs[i]->par_out;
  229. tb = lst->bsfs[i]->time_base_out;
  230. }
  231. bsf->time_base_out = tb;
  232. ret = avcodec_parameters_copy(bsf->par_out, cod_par);
  233. fail:
  234. return ret;
  235. }
  236. static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
  237. {
  238. BSFListContext *lst = bsf->priv_data;
  239. int ret;
  240. if (!lst->nb_bsfs)
  241. return ff_bsf_get_packet_ref(bsf, out);
  242. while (1) {
  243. if (lst->idx > lst->flushed_idx) {
  244. ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
  245. if (ret == AVERROR(EAGAIN)) {
  246. /* no more packets from idx-1, try with previous */
  247. lst->idx--;
  248. continue;
  249. } else if (ret == AVERROR_EOF) {
  250. /* filter idx-1 is done, continue with idx...nb_bsfs */
  251. lst->flushed_idx = lst->idx;
  252. continue;
  253. }else if (ret < 0) {
  254. /* filtering error */
  255. break;
  256. }
  257. } else {
  258. ret = ff_bsf_get_packet_ref(bsf, out);
  259. if (ret == AVERROR_EOF) {
  260. lst->idx = lst->flushed_idx;
  261. } else if (ret < 0)
  262. break;
  263. }
  264. if (lst->idx < lst->nb_bsfs) {
  265. AVPacket *pkt;
  266. if (ret == AVERROR_EOF && lst->idx == lst->flushed_idx) {
  267. /* ff_bsf_get_packet_ref returned EOF and idx is first
  268. * filter of yet not flushed filter chain */
  269. pkt = NULL;
  270. } else {
  271. pkt = out;
  272. }
  273. ret = av_bsf_send_packet(lst->bsfs[lst->idx], pkt);
  274. if (ret < 0)
  275. break;
  276. lst->idx++;
  277. } else {
  278. /* The end of filter chain, break to return result */
  279. break;
  280. }
  281. }
  282. if (ret < 0)
  283. av_packet_unref(out);
  284. return ret;
  285. }
  286. static void bsf_list_flush(AVBSFContext *bsf)
  287. {
  288. BSFListContext *lst = bsf->priv_data;
  289. for (int i = 0; i < lst->nb_bsfs; i++)
  290. av_bsf_flush(lst->bsfs[i]);
  291. lst->idx = lst->flushed_idx = 0;
  292. }
  293. static void bsf_list_close(AVBSFContext *bsf)
  294. {
  295. BSFListContext *lst = bsf->priv_data;
  296. int i;
  297. for (i = 0; i < lst->nb_bsfs; ++i)
  298. av_bsf_free(&lst->bsfs[i]);
  299. av_freep(&lst->bsfs);
  300. av_freep(&lst->item_name);
  301. }
  302. static const char *bsf_list_item_name(void *ctx)
  303. {
  304. static const char *null_filter_name = "null";
  305. AVBSFContext *bsf_ctx = ctx;
  306. BSFListContext *lst = bsf_ctx->priv_data;
  307. if (!lst->nb_bsfs)
  308. return null_filter_name;
  309. if (!lst->item_name) {
  310. int i;
  311. AVBPrint bp;
  312. av_bprint_init(&bp, 16, 128);
  313. av_bprintf(&bp, "bsf_list(");
  314. for (i = 0; i < lst->nb_bsfs; i++)
  315. av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
  316. av_bprintf(&bp, ")");
  317. av_bprint_finalize(&bp, &lst->item_name);
  318. }
  319. return lst->item_name;
  320. }
  321. static const AVClass bsf_list_class = {
  322. .class_name = "bsf_list",
  323. .item_name = bsf_list_item_name,
  324. .version = LIBAVUTIL_VERSION_INT,
  325. };
  326. const AVBitStreamFilter ff_list_bsf = {
  327. .name = "bsf_list",
  328. .priv_data_size = sizeof(BSFListContext),
  329. .priv_class = &bsf_list_class,
  330. .init = bsf_list_init,
  331. .filter = bsf_list_filter,
  332. .flush = bsf_list_flush,
  333. .close = bsf_list_close,
  334. };
  335. struct AVBSFList {
  336. AVBSFContext **bsfs;
  337. int nb_bsfs;
  338. };
  339. AVBSFList *av_bsf_list_alloc(void)
  340. {
  341. return av_mallocz(sizeof(AVBSFList));
  342. }
  343. void av_bsf_list_free(AVBSFList **lst)
  344. {
  345. int i;
  346. if (!*lst)
  347. return;
  348. for (i = 0; i < (*lst)->nb_bsfs; ++i)
  349. av_bsf_free(&(*lst)->bsfs[i]);
  350. av_free((*lst)->bsfs);
  351. av_freep(lst);
  352. }
  353. int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
  354. {
  355. return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
  356. }
  357. int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
  358. {
  359. int ret;
  360. const AVBitStreamFilter *filter;
  361. AVBSFContext *bsf;
  362. filter = av_bsf_get_by_name(bsf_name);
  363. if (!filter)
  364. return AVERROR_BSF_NOT_FOUND;
  365. ret = av_bsf_alloc(filter, &bsf);
  366. if (ret < 0)
  367. return ret;
  368. if (options) {
  369. ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
  370. if (ret < 0)
  371. goto end;
  372. }
  373. ret = av_bsf_list_append(lst, bsf);
  374. end:
  375. if (ret < 0)
  376. av_bsf_free(&bsf);
  377. return ret;
  378. }
  379. int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
  380. {
  381. int ret = 0;
  382. BSFListContext *ctx;
  383. if ((*lst)->nb_bsfs == 1) {
  384. *bsf = (*lst)->bsfs[0];
  385. av_freep(&(*lst)->bsfs);
  386. (*lst)->nb_bsfs = 0;
  387. goto end;
  388. }
  389. ret = av_bsf_alloc(&ff_list_bsf, bsf);
  390. if (ret < 0)
  391. return ret;
  392. ctx = (*bsf)->priv_data;
  393. ctx->bsfs = (*lst)->bsfs;
  394. ctx->nb_bsfs = (*lst)->nb_bsfs;
  395. end:
  396. av_freep(lst);
  397. return ret;
  398. }
  399. static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
  400. {
  401. char *bsf_name, *bsf_options_str, *buf;
  402. AVDictionary *bsf_options = NULL;
  403. int ret = 0;
  404. if (!(buf = av_strdup(str)))
  405. return AVERROR(ENOMEM);
  406. bsf_name = av_strtok(buf, "=", &bsf_options_str);
  407. if (!bsf_name) {
  408. ret = AVERROR(EINVAL);
  409. goto end;
  410. }
  411. if (bsf_options_str) {
  412. ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
  413. if (ret < 0)
  414. goto end;
  415. }
  416. ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
  417. end:
  418. av_dict_free(&bsf_options);
  419. av_free(buf);
  420. return ret;
  421. }
  422. int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
  423. {
  424. AVBSFList *lst;
  425. char *bsf_str, *buf, *dup, *saveptr;
  426. int ret;
  427. if (!str)
  428. return av_bsf_get_null_filter(bsf_lst);
  429. lst = av_bsf_list_alloc();
  430. if (!lst)
  431. return AVERROR(ENOMEM);
  432. if (!(dup = buf = av_strdup(str))) {
  433. ret = AVERROR(ENOMEM);
  434. goto end;
  435. }
  436. while (1) {
  437. bsf_str = av_strtok(buf, ",", &saveptr);
  438. if (!bsf_str)
  439. break;
  440. ret = bsf_parse_single(bsf_str, lst);
  441. if (ret < 0)
  442. goto end;
  443. buf = NULL;
  444. }
  445. ret = av_bsf_list_finalize(&lst, bsf_lst);
  446. end:
  447. if (ret < 0)
  448. av_bsf_list_free(&lst);
  449. av_free(dup);
  450. return ret;
  451. }
  452. int av_bsf_get_null_filter(AVBSFContext **bsf)
  453. {
  454. return av_bsf_alloc(&ff_list_bsf, bsf);
  455. }