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.

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