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.

556 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/avassert.h"
  20. #include "libavutil/log.h"
  21. #include "libavutil/mem.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/bprint.h"
  25. #include "avcodec.h"
  26. #include "bsf.h"
  27. #define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems)
  28. struct AVBSFInternal {
  29. AVPacket *buffer_pkt;
  30. int eof;
  31. };
  32. void av_bsf_free(AVBSFContext **pctx)
  33. {
  34. AVBSFContext *ctx;
  35. if (!pctx || !*pctx)
  36. return;
  37. ctx = *pctx;
  38. if (ctx->filter->close)
  39. ctx->filter->close(ctx);
  40. if (ctx->filter->priv_class && ctx->priv_data)
  41. av_opt_free(ctx->priv_data);
  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 char *bsf_to_name(void *bsf)
  58. {
  59. return ((AVBSFContext *)bsf)->filter->name;
  60. }
  61. static const AVClass bsf_class = {
  62. .class_name = "AVBSFContext",
  63. .item_name = bsf_to_name,
  64. .version = LIBAVUTIL_VERSION_INT,
  65. .child_next = bsf_child_next,
  66. .child_class_next = ff_bsf_child_class_next,
  67. .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
  68. };
  69. const AVClass *av_bsf_get_class(void)
  70. {
  71. return &bsf_class;
  72. }
  73. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  74. {
  75. AVBSFContext *ctx;
  76. AVBSFInternal *bsfi;
  77. int ret;
  78. ctx = av_mallocz(sizeof(*ctx));
  79. if (!ctx)
  80. return AVERROR(ENOMEM);
  81. ctx->av_class = &bsf_class;
  82. ctx->filter = filter;
  83. ctx->par_in = avcodec_parameters_alloc();
  84. ctx->par_out = avcodec_parameters_alloc();
  85. if (!ctx->par_in || !ctx->par_out) {
  86. ret = AVERROR(ENOMEM);
  87. goto fail;
  88. }
  89. bsfi = av_mallocz(sizeof(*bsfi));
  90. if (!bsfi) {
  91. ret = AVERROR(ENOMEM);
  92. goto fail;
  93. }
  94. ctx->internal = bsfi;
  95. bsfi->buffer_pkt = av_packet_alloc();
  96. if (!bsfi->buffer_pkt) {
  97. ret = AVERROR(ENOMEM);
  98. goto fail;
  99. }
  100. /* allocate priv data and init private options */
  101. if (filter->priv_data_size) {
  102. ctx->priv_data = av_mallocz(filter->priv_data_size);
  103. if (!ctx->priv_data) {
  104. ret = AVERROR(ENOMEM);
  105. goto fail;
  106. }
  107. if (filter->priv_class) {
  108. *(const AVClass **)ctx->priv_data = filter->priv_class;
  109. av_opt_set_defaults(ctx->priv_data);
  110. }
  111. }
  112. *pctx = ctx;
  113. return 0;
  114. fail:
  115. av_bsf_free(&ctx);
  116. return ret;
  117. }
  118. int av_bsf_init(AVBSFContext *ctx)
  119. {
  120. int ret, i;
  121. /* check that the codec is supported */
  122. if (ctx->filter->codec_ids) {
  123. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  124. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  125. break;
  126. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  127. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  128. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  129. "bitstream filter '%s'. Supported codecs are: ",
  130. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  131. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  132. desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
  133. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  134. desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
  135. }
  136. av_log(ctx, AV_LOG_ERROR, "\n");
  137. return AVERROR(EINVAL);
  138. }
  139. }
  140. /* initialize output parameters to be the same as input
  141. * init below might overwrite that */
  142. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  143. if (ret < 0)
  144. return ret;
  145. ctx->time_base_out = ctx->time_base_in;
  146. if (ctx->filter->init) {
  147. ret = ctx->filter->init(ctx);
  148. if (ret < 0)
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. void av_bsf_flush(AVBSFContext *ctx)
  154. {
  155. AVBSFInternal *bsfi = ctx->internal;
  156. bsfi->eof = 0;
  157. av_packet_unref(bsfi->buffer_pkt);
  158. if (ctx->filter->flush)
  159. ctx->filter->flush(ctx);
  160. }
  161. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  162. {
  163. AVBSFInternal *bsfi = ctx->internal;
  164. int ret;
  165. if (!pkt || IS_EMPTY(pkt)) {
  166. bsfi->eof = 1;
  167. return 0;
  168. }
  169. if (bsfi->eof) {
  170. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  171. return AVERROR(EINVAL);
  172. }
  173. if (!IS_EMPTY(bsfi->buffer_pkt))
  174. return AVERROR(EAGAIN);
  175. ret = av_packet_make_refcounted(pkt);
  176. if (ret < 0)
  177. return ret;
  178. av_packet_move_ref(bsfi->buffer_pkt, pkt);
  179. return 0;
  180. }
  181. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  182. {
  183. return ctx->filter->filter(ctx, pkt);
  184. }
  185. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  186. {
  187. AVBSFInternal *bsfi = ctx->internal;
  188. AVPacket *tmp_pkt;
  189. if (bsfi->eof)
  190. return AVERROR_EOF;
  191. if (IS_EMPTY(bsfi->buffer_pkt))
  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 (IS_EMPTY(bsfi->buffer_pkt))
  206. return AVERROR(EAGAIN);
  207. av_packet_move_ref(pkt, bsfi->buffer_pkt);
  208. return 0;
  209. }
  210. typedef struct BSFListContext {
  211. const AVClass *class;
  212. AVBSFContext **bsfs;
  213. int nb_bsfs;
  214. unsigned idx; // index of currently processed BSF
  215. char * item_name;
  216. } BSFListContext;
  217. static int bsf_list_init(AVBSFContext *bsf)
  218. {
  219. BSFListContext *lst = bsf->priv_data;
  220. int ret, i;
  221. const AVCodecParameters *cod_par = bsf->par_in;
  222. AVRational tb = bsf->time_base_in;
  223. for (i = 0; i < lst->nb_bsfs; ++i) {
  224. ret = avcodec_parameters_copy(lst->bsfs[i]->par_in, cod_par);
  225. if (ret < 0)
  226. goto fail;
  227. lst->bsfs[i]->time_base_in = tb;
  228. ret = av_bsf_init(lst->bsfs[i]);
  229. if (ret < 0)
  230. goto fail;
  231. cod_par = lst->bsfs[i]->par_out;
  232. tb = lst->bsfs[i]->time_base_out;
  233. }
  234. bsf->time_base_out = tb;
  235. ret = avcodec_parameters_copy(bsf->par_out, cod_par);
  236. fail:
  237. return ret;
  238. }
  239. static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out)
  240. {
  241. BSFListContext *lst = bsf->priv_data;
  242. int ret, eof = 0;
  243. if (!lst->nb_bsfs)
  244. return ff_bsf_get_packet_ref(bsf, out);
  245. while (1) {
  246. /* get a packet from the previous filter up the chain */
  247. if (lst->idx)
  248. ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out);
  249. else
  250. ret = ff_bsf_get_packet_ref(bsf, out);
  251. if (ret == AVERROR(EAGAIN)) {
  252. if (!lst->idx)
  253. return ret;
  254. lst->idx--;
  255. continue;
  256. } else if (ret == AVERROR_EOF) {
  257. eof = 1;
  258. } else if (ret < 0)
  259. return ret;
  260. /* send it to the next filter down the chain */
  261. if (lst->idx < lst->nb_bsfs) {
  262. ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out);
  263. av_assert1(ret != AVERROR(EAGAIN));
  264. if (ret < 0) {
  265. av_packet_unref(out);
  266. return ret;
  267. }
  268. lst->idx++;
  269. eof = 0;
  270. } else if (eof) {
  271. return ret;
  272. } else {
  273. return 0;
  274. }
  275. }
  276. }
  277. static void bsf_list_flush(AVBSFContext *bsf)
  278. {
  279. BSFListContext *lst = bsf->priv_data;
  280. for (int i = 0; i < lst->nb_bsfs; i++)
  281. av_bsf_flush(lst->bsfs[i]);
  282. lst->idx = 0;
  283. }
  284. static void bsf_list_close(AVBSFContext *bsf)
  285. {
  286. BSFListContext *lst = bsf->priv_data;
  287. int i;
  288. for (i = 0; i < lst->nb_bsfs; ++i)
  289. av_bsf_free(&lst->bsfs[i]);
  290. av_freep(&lst->bsfs);
  291. av_freep(&lst->item_name);
  292. }
  293. static const char *bsf_list_item_name(void *ctx)
  294. {
  295. static const char *null_filter_name = "null";
  296. AVBSFContext *bsf_ctx = ctx;
  297. BSFListContext *lst = bsf_ctx->priv_data;
  298. if (!lst->nb_bsfs)
  299. return null_filter_name;
  300. if (!lst->item_name) {
  301. int i;
  302. AVBPrint bp;
  303. av_bprint_init(&bp, 16, 128);
  304. av_bprintf(&bp, "bsf_list(");
  305. for (i = 0; i < lst->nb_bsfs; i++)
  306. av_bprintf(&bp, i ? ",%s" : "%s", lst->bsfs[i]->filter->name);
  307. av_bprintf(&bp, ")");
  308. av_bprint_finalize(&bp, &lst->item_name);
  309. }
  310. return lst->item_name;
  311. }
  312. static const AVClass bsf_list_class = {
  313. .class_name = "bsf_list",
  314. .item_name = bsf_list_item_name,
  315. .version = LIBAVUTIL_VERSION_INT,
  316. };
  317. const AVBitStreamFilter ff_list_bsf = {
  318. .name = "bsf_list",
  319. .priv_data_size = sizeof(BSFListContext),
  320. .priv_class = &bsf_list_class,
  321. .init = bsf_list_init,
  322. .filter = bsf_list_filter,
  323. .flush = bsf_list_flush,
  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. end:
  409. av_dict_free(&bsf_options);
  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 (bsf_str = av_strtok(buf, ",", &saveptr)) {
  428. ret = bsf_parse_single(bsf_str, lst);
  429. if (ret < 0)
  430. goto end;
  431. buf = NULL;
  432. }
  433. ret = av_bsf_list_finalize(&lst, bsf_lst);
  434. end:
  435. if (ret < 0)
  436. av_bsf_list_free(&lst);
  437. av_free(dup);
  438. return ret;
  439. }
  440. int av_bsf_get_null_filter(AVBSFContext **bsf)
  441. {
  442. return av_bsf_alloc(&ff_list_bsf, bsf);
  443. }