internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * copyright (c) 2001 Fabrice Bellard
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVFORMAT_INTERNAL_H
  21. #define AVFORMAT_INTERNAL_H
  22. #include <stdint.h>
  23. #include "avformat.h"
  24. #define MAX_URL_SIZE 4096
  25. #ifdef DEBUG
  26. # define hex_dump_debug(class, buf, size) av_hex_dump_log(class, AV_LOG_DEBUG, buf, size)
  27. #else
  28. # define hex_dump_debug(class, buf, size)
  29. #endif
  30. typedef struct AVCodecTag {
  31. enum AVCodecID id;
  32. unsigned int tag;
  33. } AVCodecTag;
  34. typedef struct CodecMime{
  35. char str[32];
  36. enum AVCodecID id;
  37. } CodecMime;
  38. void ff_dynarray_add(intptr_t **tab_ptr, int *nb_ptr, intptr_t elem);
  39. #ifdef __GNUC__
  40. #define dynarray_add(tab, nb_ptr, elem)\
  41. do {\
  42. __typeof__(tab) _tab = (tab);\
  43. __typeof__(elem) _elem = (elem);\
  44. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  45. ff_dynarray_add((intptr_t **)_tab, nb_ptr, (intptr_t)_elem);\
  46. } while(0)
  47. #else
  48. #define dynarray_add(tab, nb_ptr, elem)\
  49. do {\
  50. ff_dynarray_add((intptr_t **)(tab), nb_ptr, (intptr_t)(elem));\
  51. } while(0)
  52. #endif
  53. struct tm *ff_brktimegm(time_t secs, struct tm *tm);
  54. char *ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase);
  55. /**
  56. * Parse a string of hexadecimal strings. Any space between the hexadecimal
  57. * digits is ignored.
  58. *
  59. * @param data if non-null, the parsed data is written to this pointer
  60. * @param p the string to parse
  61. * @return the number of bytes written (or to be written, if data is null)
  62. */
  63. int ff_hex_to_data(uint8_t *data, const char *p);
  64. void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx);
  65. /**
  66. * Add packet to AVFormatContext->packet_buffer list, determining its
  67. * interleaved position using compare() function argument.
  68. */
  69. void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
  70. int (*compare)(AVFormatContext *, AVPacket *, AVPacket *));
  71. void ff_read_frame_flush(AVFormatContext *s);
  72. #define NTP_OFFSET 2208988800ULL
  73. #define NTP_OFFSET_US (NTP_OFFSET * 1000000ULL)
  74. /** Get the current time since NTP epoch in microseconds. */
  75. uint64_t ff_ntp_time(void);
  76. /**
  77. * Append the media-specific SDP fragment for the media stream c
  78. * to the buffer buff.
  79. *
  80. * Note, the buffer needs to be initialized, since it is appended to
  81. * existing content.
  82. *
  83. * @param buff the buffer to append the SDP fragment to
  84. * @param size the size of the buff buffer
  85. * @param st the AVStream of the media to describe
  86. * @param idx the global stream index
  87. * @param dest_addr the destination address of the media stream, may be NULL
  88. * @param dest_type the destination address type, may be NULL
  89. * @param port the destination port of the media stream, 0 if unknown
  90. * @param ttl the time to live of the stream, 0 if not multicast
  91. * @param fmt the AVFormatContext, which might contain options modifying
  92. * the generated SDP
  93. */
  94. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  95. const char *dest_addr, const char *dest_type,
  96. int port, int ttl, AVFormatContext *fmt);
  97. /**
  98. * Write a packet to another muxer than the one the user originally
  99. * intended. Useful when chaining muxers, where one muxer internally
  100. * writes a received packet to another muxer.
  101. *
  102. * @param dst the muxer to write the packet to
  103. * @param dst_stream the stream index within dst to write the packet to
  104. * @param pkt the packet to be written
  105. * @param src the muxer the packet originally was intended for
  106. * @return the value av_write_frame returned
  107. */
  108. int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
  109. AVFormatContext *src);
  110. /**
  111. * Get the length in bytes which is needed to store val as v.
  112. */
  113. int ff_get_v_length(uint64_t val);
  114. /**
  115. * Put val using a variable number of bytes.
  116. */
  117. void ff_put_v(AVIOContext *bc, uint64_t val);
  118. /**
  119. * Read a whole line of text from AVIOContext. Stop reading after reaching
  120. * either a \\n, a \\0 or EOF. The returned string is always \\0-terminated,
  121. * and may be truncated if the buffer is too small.
  122. *
  123. * @param s the read-only AVIOContext
  124. * @param buf buffer to store the read line
  125. * @param maxlen size of the buffer
  126. * @return the length of the string written in the buffer, not including the
  127. * final \\0
  128. */
  129. int ff_get_line(AVIOContext *s, char *buf, int maxlen);
  130. #define SPACE_CHARS " \t\r\n"
  131. /**
  132. * Callback function type for ff_parse_key_value.
  133. *
  134. * @param key a pointer to the key
  135. * @param key_len the number of bytes that belong to the key, including the '='
  136. * char
  137. * @param dest return the destination pointer for the value in *dest, may
  138. * be null to ignore the value
  139. * @param dest_len the length of the *dest buffer
  140. */
  141. typedef void (*ff_parse_key_val_cb)(void *context, const char *key,
  142. int key_len, char **dest, int *dest_len);
  143. /**
  144. * Parse a string with comma-separated key=value pairs. The value strings
  145. * may be quoted and may contain escaped characters within quoted strings.
  146. *
  147. * @param str the string to parse
  148. * @param callback_get_buf function that returns where to store the
  149. * unescaped value string.
  150. * @param context the opaque context pointer to pass to callback_get_buf
  151. */
  152. void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
  153. void *context);
  154. /**
  155. * Find stream index based on format-specific stream ID
  156. * @return stream index, or < 0 on error
  157. */
  158. int ff_find_stream_index(AVFormatContext *s, int id);
  159. /**
  160. * Internal version of av_index_search_timestamp
  161. */
  162. int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
  163. int64_t wanted_timestamp, int flags);
  164. /**
  165. * Internal version of av_add_index_entry
  166. */
  167. int ff_add_index_entry(AVIndexEntry **index_entries,
  168. int *nb_index_entries,
  169. unsigned int *index_entries_allocated_size,
  170. int64_t pos, int64_t timestamp, int size, int distance, int flags);
  171. /**
  172. * Add a new chapter.
  173. *
  174. * @param s media file handle
  175. * @param id unique ID for this chapter
  176. * @param start chapter start time in time_base units
  177. * @param end chapter end time in time_base units
  178. * @param title chapter title
  179. *
  180. * @return AVChapter or NULL on error
  181. */
  182. AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base,
  183. int64_t start, int64_t end, const char *title);
  184. /**
  185. * Ensure the index uses less memory than the maximum specified in
  186. * AVFormatContext.max_index_size by discarding entries if it grows
  187. * too large.
  188. */
  189. void ff_reduce_index(AVFormatContext *s, int stream_index);
  190. enum AVCodecID ff_guess_image2_codec(const char *filename);
  191. /**
  192. * Convert a date string in ISO8601 format to Unix timestamp.
  193. */
  194. int64_t ff_iso8601_to_unix_time(const char *datestr);
  195. /**
  196. * Perform a binary search using av_index_search_timestamp() and
  197. * AVInputFormat.read_timestamp().
  198. *
  199. * @param target_ts target timestamp in the time base of the given stream
  200. * @param stream_index stream number
  201. */
  202. int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
  203. int64_t target_ts, int flags);
  204. /**
  205. * Update cur_dts of all streams based on the given timestamp and AVStream.
  206. *
  207. * Stream ref_st unchanged, others set cur_dts in their native time base.
  208. * Only needed for timestamp wrapping or if (dts not set and pts!=dts).
  209. * @param timestamp new dts expressed in time_base of param ref_st
  210. * @param ref_st reference stream giving time_base of param timestamp
  211. */
  212. void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
  213. /**
  214. * Perform a binary search using read_timestamp().
  215. *
  216. * @param target_ts target timestamp in the time base of the given stream
  217. * @param stream_index stream number
  218. */
  219. int64_t ff_gen_search(AVFormatContext *s, int stream_index,
  220. int64_t target_ts, int64_t pos_min,
  221. int64_t pos_max, int64_t pos_limit,
  222. int64_t ts_min, int64_t ts_max,
  223. int flags, int64_t *ts_ret,
  224. int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
  225. /**
  226. * Set the time base and wrapping info for a given stream. This will be used
  227. * to interpret the stream's timestamps. If the new time base is invalid
  228. * (numerator or denominator are non-positive), it leaves the stream
  229. * unchanged.
  230. *
  231. * @param s stream
  232. * @param pts_wrap_bits number of bits effectively used by the pts
  233. * (used for wrap control)
  234. * @param pts_num time base numerator
  235. * @param pts_den time base denominator
  236. */
  237. void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
  238. unsigned int pts_num, unsigned int pts_den);
  239. /**
  240. * Add side data to a packet for changing parameters to the given values.
  241. * Parameters set to 0 aren't included in the change.
  242. */
  243. int ff_add_param_change(AVPacket *pkt, int32_t channels,
  244. uint64_t channel_layout, int32_t sample_rate,
  245. int32_t width, int32_t height);
  246. /**
  247. * Set the timebase for each stream from the corresponding codec timebase and
  248. * print it.
  249. */
  250. int ff_framehash_write_header(AVFormatContext *s);
  251. /**
  252. * Read a transport packet from a media file.
  253. *
  254. * @param s media file handle
  255. * @param pkt is filled
  256. * @return 0 if OK, AVERROR_xxx on error
  257. */
  258. int ff_read_packet(AVFormatContext *s, AVPacket *pkt);
  259. /**
  260. * Interleave a packet per dts in an output media file.
  261. *
  262. * Packets with pkt->destruct == av_destruct_packet will be freed inside this
  263. * function, so they cannot be used after it. Note that calling av_free_packet()
  264. * on them is still safe.
  265. *
  266. * @param s media file handle
  267. * @param out the interleaved packet will be output here
  268. * @param pkt the input packet
  269. * @param flush 1 if no further packets are available as input and all
  270. * remaining packets should be output
  271. * @return 1 if a packet was output, 0 if no packet could be output,
  272. * < 0 if an error occurred
  273. */
  274. int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
  275. AVPacket *pkt, int flush);
  276. /**
  277. * Return the frame duration in seconds. Return 0 if not available.
  278. */
  279. void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st,
  280. AVCodecParserContext *pc, AVPacket *pkt);
  281. int ff_get_audio_frame_size(AVCodecContext *enc, int size, int mux);
  282. unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id);
  283. enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag);
  284. /**
  285. * Select a PCM codec based on the given parameters.
  286. *
  287. * @param bps bits-per-sample
  288. * @param flt floating-point
  289. * @param be big-endian
  290. * @param sflags signed flags. each bit corresponds to one byte of bit depth.
  291. * e.g. the 1st bit indicates if 8-bit should be signed or
  292. * unsigned, the 2nd bit indicates if 16-bit should be signed or
  293. * unsigned, etc... This is useful for formats such as WAVE where
  294. * only 8-bit is unsigned and all other bit depths are signed.
  295. * @return a PCM codec id or AV_CODEC_ID_NONE
  296. */
  297. enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags);
  298. #endif /* AVFORMAT_INTERNAL_H */