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.

575 lines
14KB

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