bsf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. av_opt_free(ctx);
  41. if (ctx->internal)
  42. av_packet_free(&ctx->internal->buffer_pkt);
  43. av_freep(&ctx->internal);
  44. av_freep(&ctx->priv_data);
  45. avcodec_parameters_free(&ctx->par_in);
  46. avcodec_parameters_free(&ctx->par_out);
  47. av_freep(pctx);
  48. }
  49. static void *bsf_child_next(void *obj, void *prev)
  50. {
  51. AVBSFContext *ctx = obj;
  52. if (!prev && ctx->filter->priv_class)
  53. return ctx->priv_data;
  54. return NULL;
  55. }
  56. static const AVClass bsf_class = {
  57. .class_name = "AVBSFContext",
  58. .item_name = av_default_item_name,
  59. .version = LIBAVUTIL_VERSION_INT,
  60. .child_next = bsf_child_next,
  61. .child_class_next = ff_bsf_child_class_next,
  62. };
  63. const AVClass *av_bsf_get_class(void)
  64. {
  65. return &bsf_class;
  66. }
  67. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  68. {
  69. AVBSFContext *ctx;
  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. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  83. if (!ctx->internal) {
  84. ret = AVERROR(ENOMEM);
  85. goto fail;
  86. }
  87. ctx->internal->buffer_pkt = av_packet_alloc();
  88. if (!ctx->internal->buffer_pkt) {
  89. ret = AVERROR(ENOMEM);
  90. goto fail;
  91. }
  92. av_opt_set_defaults(ctx);
  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. ctx->internal->eof = 0;
  149. av_packet_unref(ctx->internal->buffer_pkt);
  150. if (ctx->filter->flush)
  151. ctx->filter->flush(ctx);
  152. }
  153. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  154. {
  155. int ret;
  156. if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
  157. ctx->internal->eof = 1;
  158. return 0;
  159. }
  160. if (ctx->internal->eof) {
  161. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  162. return AVERROR(EINVAL);
  163. }
  164. if (ctx->internal->buffer_pkt->data ||
  165. ctx->internal->buffer_pkt->side_data_elems)
  166. return AVERROR(EAGAIN);
  167. ret = av_packet_make_refcounted(pkt);
  168. if (ret < 0)
  169. return ret;
  170. av_packet_move_ref(ctx->internal->buffer_pkt, pkt);
  171. return 0;
  172. }
  173. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  174. {
  175. return ctx->filter->filter(ctx, pkt);
  176. }
  177. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  178. {
  179. AVBSFInternal *in = ctx->internal;
  180. AVPacket *tmp_pkt;
  181. if (in->eof)
  182. return AVERROR_EOF;
  183. if (!ctx->internal->buffer_pkt->data &&
  184. !ctx->internal->buffer_pkt->side_data_elems)
  185. return AVERROR(EAGAIN);
  186. tmp_pkt = av_packet_alloc();
  187. if (!tmp_pkt)
  188. return AVERROR(ENOMEM);
  189. *pkt = ctx->internal->buffer_pkt;
  190. ctx->internal->buffer_pkt = tmp_pkt;
  191. return 0;
  192. }
  193. int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
  194. {
  195. AVBSFInternal *in = ctx->internal;
  196. if (in->eof)
  197. return AVERROR_EOF;
  198. if (!ctx->internal->buffer_pkt->data &&
  199. !ctx->internal->buffer_pkt->side_data_elems)
  200. return AVERROR(EAGAIN);
  201. av_packet_move_ref(pkt, ctx->internal->buffer_pkt);
  202. return 0;
  203. }
  204. typedef struct BSFListContext {
  205. const AVClass *class;
  206. AVBSFContext **bsfs;
  207. int nb_bsfs;
  208. unsigned idx; // index of currently processed BSF
  209. unsigned flushed_idx; // index of BSF being flushed
  210. char * item_name;
  211. } BSFListContext;
  212. static int bsf_list_init(AVBSFContext *bsf)
  213. {
  214. BSFListContext *lst = bsf->priv_data;
  215. int ret, i;
  216. const AVCodecParameters *cod_par = bsf->par_in;
  217. AVRational tb = bsf->time_base_in;
  218. for (i = 0; i < lst->nb_bsfs; ++i) {
  219. ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
  220. if (ret < 0)
  221. goto fail;
  222. lst->bsfs[i]->time_base_in = tb;
  223. ret = av_bsf_init(lst->bsfs[i]);
  224. if (ret < 0)
  225. goto fail;
  226. cod_par = lst->bsfs[i]->par_out;
  227. tb = lst->bsfs[i]->time_base_out;
  228. }
  229. bsf->time_base_out = tb;
  230. ret = avcodec_parameters_copy(bsf->par_out, cod_par);
  231. fail:
  232. return ret;
  233. }
  234. static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
  235. {
  236. BSFListContext *lst = bsf->priv_data;
  237. int ret;
  238. if (!lst->nb_bsfs)
  239. return ff_bsf_get_packet_ref(bsf, out);
  240. while (1) {
  241. if (lst->idx > lst->flushed_idx) {
  242. ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
  243. if (ret == AVERROR(EAGAIN)) {
  244. /* no more packets from idx-1, try with previous */
  245. ret = 0;
  246. lst->idx--;
  247. continue;
  248. } else if (ret == AVERROR_EOF) {
  249. /* filter idx-1 is done, continue with idx...nb_bsfs */
  250. lst->flushed_idx = lst->idx;
  251. continue;
  252. }else if (ret < 0) {
  253. /* filtering error */
  254. break;
  255. }
  256. } else {
  257. ret = ff_bsf_get_packet_ref(bsf, out);
  258. if (ret == AVERROR_EOF) {
  259. lst->idx = lst->flushed_idx;
  260. } else if (ret < 0)
  261. break;
  262. }
  263. if (lst->idx < lst->nb_bsfs) {
  264. AVPacket *pkt;
  265. if (ret == AVERROR_EOF && lst->idx == lst->flushed_idx) {
  266. /* ff_bsf_get_packet_ref returned EOF and idx is first
  267. * filter of yet not flushed filter chain */
  268. pkt = NULL;
  269. } else {
  270. pkt = out;
  271. }
  272. ret = av_bsf_send_packet(lst->bsfs[lst->idx], pkt);
  273. if (ret < 0)
  274. break;
  275. lst->idx++;
  276. } else {
  277. /* The end of filter chain, break to return result */
  278. break;
  279. }
  280. }
  281. if (ret < 0)
  282. av_packet_unref(out);
  283. return ret;
  284. }
  285. static void bsf_list_close(AVBSFContext *bsf)
  286. {
  287. BSFListContext *lst = bsf->priv_data;
  288. int i;
  289. for (i = 0; i < lst->nb_bsfs; ++i)
  290. av_bsf_free(&lst->bsfs[i]);
  291. av_freep(&lst->bsfs);
  292. av_freep(&lst->item_name);
  293. }
  294. static const char *bsf_list_item_name(void *ctx)
  295. {
  296. static const char *null_filter_name = "null";
  297. AVBSFContext *bsf_ctx = ctx;
  298. BSFListContext *lst = bsf_ctx->priv_data;
  299. if (!lst->nb_bsfs)
  300. return null_filter_name;
  301. if (!lst->item_name) {
  302. int i;
  303. AVBPrint bp;
  304. av_bprint_init(&bp, 16, 128);
  305. av_bprintf(&bp, "bsf_list(");
  306. for (i = 0; i < lst->nb_bsfs; i++)
  307. av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
  308. av_bprintf(&bp, ")");
  309. av_bprint_finalize(&bp, &lst->item_name);
  310. }
  311. return lst->item_name;
  312. }
  313. static const AVClass bsf_list_class = {
  314. .class_name = "bsf_list",
  315. .item_name = bsf_list_item_name,
  316. .version = LIBAVUTIL_VERSION_INT,
  317. };
  318. const AVBitStreamFilter ff_list_bsf = {
  319. .name = "bsf_list",
  320. .priv_data_size = sizeof(BSFListContext),
  321. .priv_class = &bsf_list_class,
  322. .init = bsf_list_init,
  323. .filter = bsf_list_filter,
  324. .close = bsf_list_close,
  325. };
  326. struct AVBSFList {
  327. AVBSFContext **bsfs;
  328. int nb_bsfs;
  329. };
  330. AVBSFList *av_bsf_list_alloc(void)
  331. {
  332. return av_mallocz(sizeof(AVBSFList));
  333. }
  334. void av_bsf_list_free(AVBSFList **lst)
  335. {
  336. int i;
  337. if (!*lst)
  338. return;
  339. for (i = 0; i < (*lst)->nb_bsfs; ++i)
  340. av_bsf_free(&(*lst)->bsfs[i]);
  341. av_free((*lst)->bsfs);
  342. av_freep(lst);
  343. }
  344. int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
  345. {
  346. return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
  347. }
  348. int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
  349. {
  350. int ret;
  351. const AVBitStreamFilter *filter;
  352. AVBSFContext *bsf;
  353. filter = av_bsf_get_by_name(bsf_name);
  354. if (!filter)
  355. return AVERROR_BSF_NOT_FOUND;
  356. ret = av_bsf_alloc(filter, &bsf);
  357. if (ret < 0)
  358. return ret;
  359. if (options) {
  360. ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
  361. if (ret < 0)
  362. goto end;
  363. }
  364. ret = av_bsf_list_append(lst, bsf);
  365. end:
  366. if (ret < 0)
  367. av_bsf_free(&bsf);
  368. return ret;
  369. }
  370. int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
  371. {
  372. int ret = 0;
  373. BSFListContext *ctx;
  374. if ((*lst)->nb_bsfs == 1) {
  375. *bsf = (*lst)->bsfs[0];
  376. av_freep(&(*lst)->bsfs);
  377. (*lst)->nb_bsfs = 0;
  378. goto end;
  379. }
  380. ret = av_bsf_alloc(&ff_list_bsf, bsf);
  381. if (ret < 0)
  382. return ret;
  383. ctx = (*bsf)->priv_data;
  384. ctx->bsfs = (*lst)->bsfs;
  385. ctx->nb_bsfs = (*lst)->nb_bsfs;
  386. end:
  387. av_freep(lst);
  388. return ret;
  389. }
  390. static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
  391. {
  392. char *bsf_name, *bsf_options_str, *buf;
  393. AVDictionary *bsf_options = NULL;
  394. int ret = 0;
  395. if (!(buf = av_strdup(str)))
  396. return AVERROR(ENOMEM);
  397. bsf_name = av_strtok(buf, "=", &bsf_options_str);
  398. if (!bsf_name) {
  399. ret = AVERROR(EINVAL);
  400. goto end;
  401. }
  402. if (bsf_options_str) {
  403. ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
  404. if (ret < 0)
  405. goto end;
  406. }
  407. ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
  408. av_dict_free(&bsf_options);
  409. end:
  410. av_free(buf);
  411. return ret;
  412. }
  413. int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
  414. {
  415. AVBSFList *lst;
  416. char *bsf_str, *buf, *dup, *saveptr;
  417. int ret;
  418. if (!str)
  419. return av_bsf_get_null_filter(bsf_lst);
  420. lst = av_bsf_list_alloc();
  421. if (!lst)
  422. return AVERROR(ENOMEM);
  423. if (!(dup = buf = av_strdup(str))) {
  424. ret = AVERROR(ENOMEM);
  425. goto end;
  426. }
  427. while (1) {
  428. bsf_str = av_strtok(buf, ",", &saveptr);
  429. if (!bsf_str)
  430. break;
  431. ret = bsf_parse_single(bsf_str, lst);
  432. if (ret < 0)
  433. goto end;
  434. buf = NULL;
  435. }
  436. ret = av_bsf_list_finalize(&lst, bsf_lst);
  437. end:
  438. if (ret < 0)
  439. av_bsf_list_free(&lst);
  440. av_free(dup);
  441. return ret;
  442. }
  443. int av_bsf_get_null_filter(AVBSFContext **bsf)
  444. {
  445. return av_bsf_alloc(&ff_list_bsf, bsf);
  446. }