formats.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Filter layer - format negotiation
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/pixdesc.h"
  22. #include "avfilter.h"
  23. /**
  24. * Add all refs from a to ret and destroy a.
  25. */
  26. static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
  27. {
  28. int i;
  29. for(i = 0; i < a->refcount; i ++) {
  30. ret->refs[ret->refcount] = a->refs[i];
  31. *ret->refs[ret->refcount++] = ret;
  32. }
  33. av_free(a->refs);
  34. av_free(a->formats);
  35. av_free(a);
  36. }
  37. AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
  38. {
  39. AVFilterFormats *ret;
  40. unsigned i, j, k = 0;
  41. ret = av_mallocz(sizeof(AVFilterFormats));
  42. /* merge list of formats */
  43. ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
  44. b->format_count));
  45. for(i = 0; i < a->format_count; i ++)
  46. for(j = 0; j < b->format_count; j ++)
  47. if(a->formats[i] == b->formats[j])
  48. ret->formats[k++] = a->formats[i];
  49. ret->format_count = k;
  50. /* check that there was at least one common format */
  51. if(!ret->format_count) {
  52. av_free(ret->formats);
  53. av_free(ret);
  54. return NULL;
  55. }
  56. ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
  57. merge_ref(ret, a);
  58. merge_ref(ret, b);
  59. return ret;
  60. }
  61. AVFilterFormats *avfilter_make_format_list(const int *fmts)
  62. {
  63. AVFilterFormats *formats;
  64. int count;
  65. for (count = 0; fmts[count] != -1; count++)
  66. ;
  67. formats = av_mallocz(sizeof(AVFilterFormats));
  68. formats->formats = av_malloc(sizeof(*formats->formats) * count);
  69. formats->format_count = count;
  70. memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
  71. return formats;
  72. }
  73. int avfilter_add_format(AVFilterFormats **avff, int fmt)
  74. {
  75. int *fmts;
  76. if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
  77. return AVERROR(ENOMEM);
  78. fmts = av_realloc((*avff)->formats,
  79. sizeof((*avff)->formats) * ((*avff)->format_count+1));
  80. if (!fmts)
  81. return AVERROR(ENOMEM);
  82. (*avff)->formats = fmts;
  83. (*avff)->formats[(*avff)->format_count++] = fmt;
  84. return 0;
  85. }
  86. AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
  87. {
  88. AVFilterFormats *ret = NULL;
  89. int fmt;
  90. int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
  91. type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
  92. for (fmt = 0; fmt < num_formats; fmt++)
  93. if ((type != AVMEDIA_TYPE_VIDEO) ||
  94. (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
  95. avfilter_add_format(&ret, fmt);
  96. return ret;
  97. }
  98. void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
  99. {
  100. *ref = f;
  101. f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
  102. f->refs[f->refcount-1] = ref;
  103. }
  104. static int find_ref_index(AVFilterFormats **ref)
  105. {
  106. int i;
  107. for(i = 0; i < (*ref)->refcount; i ++)
  108. if((*ref)->refs[i] == ref)
  109. return i;
  110. return -1;
  111. }
  112. void avfilter_formats_unref(AVFilterFormats **ref)
  113. {
  114. int idx;
  115. if (!*ref)
  116. return;
  117. idx = find_ref_index(ref);
  118. if(idx >= 0)
  119. memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
  120. sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
  121. if(!--(*ref)->refcount) {
  122. av_free((*ref)->formats);
  123. av_free((*ref)->refs);
  124. av_free(*ref);
  125. }
  126. *ref = NULL;
  127. }
  128. void avfilter_formats_changeref(AVFilterFormats **oldref,
  129. AVFilterFormats **newref)
  130. {
  131. int idx = find_ref_index(oldref);
  132. if(idx >= 0) {
  133. (*oldref)->refs[idx] = newref;
  134. *newref = *oldref;
  135. *oldref = NULL;
  136. }
  137. }