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.

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