PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libavformat/format.c

https://github.com/hhool/FFmpeg
C | 189 lines | 140 code | 21 blank | 28 comment | 48 complexity | b00a085bd9d0bdd0f1c64c27652d1b98 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0, GPL-2.0, GPL-3.0
  1. /*
  2. * Format register and lookup
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "internal.h"
  23. #include "libavutil/atomic.h"
  24. #include "libavutil/avstring.h"
  25. /**
  26. * @file
  27. * Format register and lookup
  28. */
  29. /** head of registered input format linked list */
  30. static AVInputFormat *first_iformat = NULL;
  31. /** head of registered output format linked list */
  32. static AVOutputFormat *first_oformat = NULL;
  33. static AVInputFormat **last_iformat = &first_iformat;
  34. static AVOutputFormat **last_oformat = &first_oformat;
  35. AVInputFormat *av_iformat_next(AVInputFormat *f)
  36. {
  37. if (f)
  38. return f->next;
  39. else
  40. return first_iformat;
  41. }
  42. AVOutputFormat *av_oformat_next(AVOutputFormat *f)
  43. {
  44. if (f)
  45. return f->next;
  46. else
  47. return first_oformat;
  48. }
  49. void av_register_input_format(AVInputFormat *format)
  50. {
  51. AVInputFormat **p = last_iformat;
  52. format->next = NULL;
  53. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
  54. p = &(*p)->next;
  55. last_iformat = &format->next;
  56. }
  57. void av_register_output_format(AVOutputFormat *format)
  58. {
  59. AVOutputFormat **p = last_oformat;
  60. format->next = NULL;
  61. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
  62. p = &(*p)->next;
  63. last_oformat = &format->next;
  64. }
  65. int av_match_ext(const char *filename, const char *extensions)
  66. {
  67. const char *ext, *p;
  68. char ext1[32], *q;
  69. if (!filename)
  70. return 0;
  71. ext = strrchr(filename, '.');
  72. if (ext) {
  73. ext++;
  74. p = extensions;
  75. for (;;) {
  76. q = ext1;
  77. while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
  78. *q++ = *p++;
  79. *q = '\0';
  80. if (!av_strcasecmp(ext1, ext))
  81. return 1;
  82. if (*p == '\0')
  83. break;
  84. p++;
  85. }
  86. }
  87. return 0;
  88. }
  89. static int match_format(const char *name, const char *names)
  90. {
  91. const char *p;
  92. int len, namelen;
  93. if (!name || !names)
  94. return 0;
  95. namelen = strlen(name);
  96. while ((p = strchr(names, ','))) {
  97. len = FFMAX(p - names, namelen);
  98. if (!av_strncasecmp(name, names, len))
  99. return 1;
  100. names = p + 1;
  101. }
  102. return !av_strcasecmp(name, names);
  103. }
  104. AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
  105. const char *mime_type)
  106. {
  107. AVOutputFormat *fmt = NULL, *fmt_found;
  108. int score_max, score;
  109. /* specific test for image sequences */
  110. #if CONFIG_IMAGE2_MUXER
  111. if (!short_name && filename &&
  112. av_filename_number_test(filename) &&
  113. ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
  114. return av_guess_format("image2", NULL, NULL);
  115. }
  116. #endif
  117. /* Find the proper file type. */
  118. fmt_found = NULL;
  119. score_max = 0;
  120. while ((fmt = av_oformat_next(fmt))) {
  121. score = 0;
  122. if (fmt->name && short_name && match_format(short_name, fmt->name))
  123. score += 100;
  124. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  125. score += 10;
  126. if (filename && fmt->extensions &&
  127. av_match_ext(filename, fmt->extensions)) {
  128. score += 5;
  129. }
  130. if (score > score_max) {
  131. score_max = score;
  132. fmt_found = fmt;
  133. }
  134. }
  135. return fmt_found;
  136. }
  137. enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  138. const char *filename, const char *mime_type,
  139. enum AVMediaType type)
  140. {
  141. if (!strcmp(fmt->name, "segment") || !strcmp(fmt->name, "ssegment")) {
  142. fmt = av_guess_format(NULL, filename, NULL);
  143. }
  144. if (type == AVMEDIA_TYPE_VIDEO) {
  145. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  146. #if CONFIG_IMAGE2_MUXER
  147. if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
  148. codec_id = ff_guess_image2_codec(filename);
  149. }
  150. #endif
  151. if (codec_id == AV_CODEC_ID_NONE)
  152. codec_id = fmt->video_codec;
  153. return codec_id;
  154. } else if (type == AVMEDIA_TYPE_AUDIO)
  155. return fmt->audio_codec;
  156. else if (type == AVMEDIA_TYPE_SUBTITLE)
  157. return fmt->subtitle_codec;
  158. else
  159. return AV_CODEC_ID_NONE;
  160. }
  161. AVInputFormat *av_find_input_format(const char *short_name)
  162. {
  163. AVInputFormat *fmt = NULL;
  164. while ((fmt = av_iformat_next(fmt)))
  165. if (match_format(short_name, fmt->name))
  166. return fmt;
  167. return NULL;
  168. }