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.

573 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. 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. AVBSFInternal *bsfi;
  71. int ret;
  72. ctx = av_mallocz(sizeof(*ctx));
  73. if (!ctx)
  74. return AVERROR(ENOMEM);
  75. ctx->av_class = &bsf_class;
  76. ctx->filter = filter;
  77. ctx->par_in = avcodec_parameters_alloc();
  78. ctx->par_out = avcodec_parameters_alloc();
  79. if (!ctx->par_in || !ctx->par_out) {
  80. ret = AVERROR(ENOMEM);
  81. goto fail;
  82. }
  83. bsfi = av_mallocz(sizeof(*bsfi));
  84. if (!bsfi) {
  85. ret = AVERROR(ENOMEM);
  86. goto fail;
  87. }
  88. ctx->internal = bsfi;
  89. bsfi->buffer_pkt = av_packet_alloc();
  90. if (!bsfi->buffer_pkt) {
  91. ret = AVERROR(ENOMEM);
  92. goto fail;
  93. }
  94. av_opt_set_defaults(ctx);
  95. /* allocate priv data and init private options */
  96. if (filter->priv_data_size) {
  97. ctx->priv_data = av_mallocz(filter->priv_data_size);
  98. if (!ctx->priv_data) {
  99. ret = AVERROR(ENOMEM);
  100. goto fail;
  101. }
  102. if (filter->priv_class) {
  103. *(const AVClass **)ctx->priv_data = filter->priv_class;
  104. av_opt_set_defaults(ctx->priv_data);
  105. }
  106. }
  107. *pctx = ctx;
  108. return 0;
  109. fail:
  110. av_bsf_free(&ctx);
  111. return ret;
  112. }
  113. int av_bsf_init(AVBSFContext *ctx)
  114. {
  115. int ret, i;
  116. /* check that the codec is supported */
  117. if (ctx->filter->codec_ids) {
  118. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  119. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  120. break;
  121. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  122. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  123. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  124. "bitstream filter '%s'. Supported codecs are: ",
  125. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  126. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  127. desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
  128. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  129. desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
  130. }
  131. av_log(ctx, AV_LOG_ERROR, "\n");
  132. return AVERROR(EINVAL);
  133. }
  134. }
  135. /* initialize output parameters to be the same as input
  136. * init below might overwrite that */
  137. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  138. if (ret < 0)
  139. return ret;
  140. ctx->time_base_out = ctx->time_base_in;
  141. if (ctx->filter->init) {
  142. ret = ctx->filter->init(ctx);
  143. if (ret < 0)
  144. return ret;
  145. }
  146. return 0;
  147. }
  148. void av_bsf_flush(AVBSFContext *ctx)
  149. {
  150. AVBSFInternal *bsfi = ctx->internal;
  151. bsfi->eof = 0;
  152. av_packet_unref(bsfi->buffer_pkt);
  153. if (ctx->filter->flush)
  154. ctx->filter->flush(ctx);
  155. }
  156. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  157. {
  158. AVBSFInternal *bsfi = ctx->internal;
  159. int ret;
  160. if (!pkt || (!pkt->data && !pkt->side_data_elems)) {
  161. bsfi->eof = 1;
  162. return 0;
  163. }
  164. if (bsfi->eof) {
  165. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  166. return AVERROR(EINVAL);
  167. }
  168. if (bsfi->buffer_pkt->data ||
  169. bsfi->buffer_pkt->side_data_elems)
  170. return AVERROR(EAGAIN);
  171. ret = av_packet_make_refcounted(pkt);
  172. if (ret < 0)
  173. return ret;
  174. av_packet_move_ref(bsfi->buffer_pkt, pkt);
  175. return 0;
  176. }
  177. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  178. {
  179. return ctx->filter->filter(ctx, pkt);
  180. }
  181. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  182. {
  183. AVBSFInternal *bsfi = ctx->internal;
  184. AVPacket *tmp_pkt;
  185. if (bsfi->eof)
  186. return AVERROR_EOF;
  187. if (!bsfi->buffer_pkt->data &&
  188. !bsfi->buffer_pkt->side_data_elems)
  189. return AVERROR(EAGAIN);
  190. tmp_pkt = av_packet_alloc();
  191. if (!tmp_pkt)
  192. return AVERROR(ENOMEM);
  193. *pkt = bsfi->buffer_pkt;
  194. bsfi->buffer_pkt = tmp_pkt;
  195. return 0;
  196. }
  197. int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
  198. {
  199. AVBSFInternal *bsfi = ctx->internal;
  200. if (bsfi->eof)
  201. return AVERROR_EOF;
  202. if (!bsfi->buffer_pkt->data &&
  203. !bsfi->buffer_pkt->side_data_elems)
  204. return AVERROR(EAGAIN);
  205. av_packet_move_ref(pkt, bsfi->buffer_pkt);
  206. return 0;
  207. }
  208. typedef struct BSFListContext {
  209. const AVClass *class;
  210. AVBSFContext **bsfs;
  211. int nb_bsfs;
  212. unsigned idx; // index of currently processed BSF
  213. unsigned flushed_idx; // index of BSF being flushed
  214. char * item_name;
  215. } BSFListContext;
  216. static int bsf_list_init(AVBSFContext *bsf)
  217. {
  218. BSFListContext *lst = bsf->priv_data;
  219. int ret, i;
  220. const AVCodecParameters *cod_par = bsf->par_in;
  221. AVRational tb = bsf->time_base_in;
  222. for (i = 0; i < lst->nb_bsfs; ++i) {
  223. ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
  224. if (ret < 0)
  225. goto fail;
  226. lst->bsfs[i]->time_base_in = tb;
  227. ret = av_bsf_init(lst->bsfs[i]);
  228. if (ret < 0)
  229. goto fail;
  230. cod_par = lst->bsfs[i]->par_out;
  231. tb = lst->bsfs[i]->time_base_out;
  232. }
  233. bsf->time_base_out = tb;
  234. ret = avcodec_parameters_copy(bsf->par_out, cod_par);
  235. fail:
  236. return ret;
  237. }
  238. static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
  239. {
  240. BSFListContext *lst = bsf->priv_data;
  241. int ret;
  242. if (!lst->nb_bsfs)
  243. return ff_bsf_get_packet_ref(bsf, out);
  244. while (1) {
  245. if (lst->idx > lst->flushed_idx) {
  246. ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
  247. if (ret == AVERROR(EAGAIN)) {
  248. /* no more packets from idx-1, try with previous */
  249. lst->idx--;
  250. continue;
  251. } else if (ret == AVERROR_EOF) {
  252. /* filter idx-1 is done, continue with idx...nb_bsfs */
  253. lst->flushed_idx = lst->idx;
  254. continue;
  255. }else if (ret < 0) {
  256. /* filtering error */
  257. break;
  258. }
  259. } else {
  260. ret = ff_bsf_get_packet_ref(bsf, out);
  261. if (ret == AVERROR_EOF) {
  262. lst->idx = lst->flushed_idx;
  263. } else if (ret < 0)
  264. break;
  265. }
  266. if (lst->idx < lst->nb_bsfs) {
  267. AVPacket *pkt;
  268. if (ret == AVERROR_EOF && lst->idx == lst->flushed_idx) {
  269. /* ff_bsf_get_packet_ref returned EOF and idx is first
  270. * filter of yet not flushed filter chain */
  271. pkt = NULL;
  272. } else {
  273. pkt = out;
  274. }
  275. ret = av_bsf_send_packet(lst->bsfs[lst->idx], pkt);
  276. if (ret < 0)
  277. break;
  278. lst->idx++;
  279. } else {
  280. /* The end of filter chain, break to return result */
  281. break;
  282. }
  283. }
  284. if (ret < 0)
  285. av_packet_unref(out);
  286. return ret;
  287. }
  288. static void bsf_list_flush(AVBSFContext *bsf)
  289. {
  290. BSFListContext *lst = bsf->priv_data;
  291. for (int i = 0; i < lst->nb_bsfs; i++)
  292. av_bsf_flush(lst->bsfs[i]);
  293. lst->idx = lst->flushed_idx = 0;
  294. }
  295. static void bsf_list_close(AVBSFContext *bsf)
  296. {
  297. BSFListContext *lst = bsf->priv_data;
  298. int i;
  299. for (i = 0; i < lst->nb_bsfs; ++i)
  300. av_bsf_free(&lst->bsfs[i]);
  301. av_freep(&lst->bsfs);
  302. av_freep(&lst->item_name);
  303. }
  304. static const char *bsf_list_item_name(void *ctx)
  305. {
  306. static const char *null_filter_name = "null";
  307. AVBSFContext *bsf_ctx = ctx;
  308. BSFListContext *lst = bsf_ctx->priv_data;
  309. if (!lst->nb_bsfs)
  310. return null_filter_name;
  311. if (!lst->item_name) {
  312. int i;
  313. AVBPrint bp;
  314. av_bprint_init(&bp, 16, 128);
  315. av_bprintf(&bp, "bsf_list(");
  316. for (i = 0; i < lst->nb_bsfs; i++)
  317. av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
  318. av_bprintf(&bp, ")");
  319. av_bprint_finalize(&bp, &lst->item_name);
  320. }
  321. return lst->item_name;
  322. }
  323. static const AVClass bsf_list_class = {
  324. .class_name = "bsf_list",
  325. .item_name = bsf_list_item_name,
  326. .version = LIBAVUTIL_VERSION_INT,
  327. };
  328. const AVBitStreamFilter ff_list_bsf = {
  329. .name = "bsf_list",
  330. .priv_data_size = sizeof(BSFListContext),
  331. .priv_class = &bsf_list_class,
  332. .init = bsf_list_init,
  333. .filter = bsf_list_filter,
  334. .flush = bsf_list_flush,
  335. .close = bsf_list_close,
  336. };
  337. struct AVBSFList {
  338. AVBSFContext **bsfs;
  339. int nb_bsfs;
  340. };
  341. AVBSFList *av_bsf_list_alloc(void)
  342. {
  343. return av_mallocz(sizeof(AVBSFList));
  344. }
  345. void av_bsf_list_free(AVBSFList **lst)
  346. {
  347. int i;
  348. if (!*lst)
  349. return;
  350. for (i = 0; i < (*lst)->nb_bsfs; ++i)
  351. av_bsf_free(&(*lst)->bsfs[i]);
  352. av_free((*lst)->bsfs);
  353. av_freep(lst);
  354. }
  355. int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf)
  356. {
  357. return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf);
  358. }
  359. int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options)
  360. {
  361. int ret;
  362. const AVBitStreamFilter *filter;
  363. AVBSFContext *bsf;
  364. filter = av_bsf_get_by_name(bsf_name);
  365. if (!filter)
  366. return AVERROR_BSF_NOT_FOUND;
  367. ret = av_bsf_alloc(filter, &bsf);
  368. if (ret < 0)
  369. return ret;
  370. if (options) {
  371. ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN);
  372. if (ret < 0)
  373. goto end;
  374. }
  375. ret = av_bsf_list_append(lst, bsf);
  376. end:
  377. if (ret < 0)
  378. av_bsf_free(&bsf);
  379. return ret;
  380. }
  381. int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf)
  382. {
  383. int ret = 0;
  384. BSFListContext *ctx;
  385. if ((*lst)->nb_bsfs == 1) {
  386. *bsf = (*lst)->bsfs[0];
  387. av_freep(&(*lst)->bsfs);
  388. (*lst)->nb_bsfs = 0;
  389. goto end;
  390. }
  391. ret = av_bsf_alloc(&ff_list_bsf, bsf);
  392. if (ret < 0)
  393. return ret;
  394. ctx = (*bsf)->priv_data;
  395. ctx->bsfs = (*lst)->bsfs;
  396. ctx->nb_bsfs = (*lst)->nb_bsfs;
  397. end:
  398. av_freep(lst);
  399. return ret;
  400. }
  401. static int bsf_parse_single(const char *str, AVBSFList *bsf_lst)
  402. {
  403. char *bsf_name, *bsf_options_str, *buf;
  404. AVDictionary *bsf_options = NULL;
  405. int ret = 0;
  406. if (!(buf = av_strdup(str)))
  407. return AVERROR(ENOMEM);
  408. bsf_name = av_strtok(buf, "=", &bsf_options_str);
  409. if (!bsf_name) {
  410. ret = AVERROR(EINVAL);
  411. goto end;
  412. }
  413. if (bsf_options_str) {
  414. ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0);
  415. if (ret < 0)
  416. goto end;
  417. }
  418. ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options);
  419. end:
  420. av_dict_free(&bsf_options);
  421. av_free(buf);
  422. return ret;
  423. }
  424. int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
  425. {
  426. AVBSFList *lst;
  427. char *bsf_str, *buf, *dup, *saveptr;
  428. int ret;
  429. if (!str)
  430. return av_bsf_get_null_filter(bsf_lst);
  431. lst = av_bsf_list_alloc();
  432. if (!lst)
  433. return AVERROR(ENOMEM);
  434. if (!(dup = buf = av_strdup(str))) {
  435. ret = AVERROR(ENOMEM);
  436. goto end;
  437. }
  438. while (1) {
  439. bsf_str = av_strtok(buf, ",", &saveptr);
  440. if (!bsf_str)
  441. break;
  442. ret = bsf_parse_single(bsf_str, lst);
  443. if (ret < 0)
  444. goto end;
  445. buf = NULL;
  446. }
  447. ret = av_bsf_list_finalize(&lst, bsf_lst);
  448. end:
  449. if (ret < 0)
  450. av_bsf_list_free(&lst);
  451. av_free(dup);
  452. return ret;
  453. }
  454. int av_bsf_get_null_filter(AVBSFContext **bsf)
  455. {
  456. return av_bsf_alloc(&ff_list_bsf, bsf);
  457. }