### FFmpeg Example Setup Source: https://ffmpeg.org/doxygen/4.4/tests_2avpacket_8c.html Includes necessary headers for FFmpeg operations and standard C libraries. This setup is common for examples interacting with FFmpeg's avcodec and avutil modules. ```c #include #include #include #include #include "libavcodec/avcodec.h" #include "libavutil/error.h" ``` -------------------------------- ### Setup RTSP Output Streams Source: https://ffmpeg.org/doxygen/4.4/rtspenc_8c_source.html Configures the RTSP output streams and generates the SDP description. It handles setting the start time and constructing the SDP based on the provided address. ```c #include "libavutil/time.h" #include "url.h" static const AVClass rtsp_muxer_class = { .class_name = "RTSP muxer", .item_name = av_default_item_name, .option = ff_rtsp_options, .version = LIBAVUTIL_VERSION_INT, }; int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr) { RTSPState *rt = s->priv_data; RTSPMessageHeader reply1, *reply = &reply1; int i; char *sdp; AVFormatContext sdp_ctx, *ctx_array[1]; char url[MAX_URL_SIZE]; if (s->start_time_realtime == 0 || s->start_time_realtime == AV_NOPTS_VALUE) s->start_time_realtime = av_gettime(); /* Announce the stream */ sdp = av_mallocz(SDP_MAX_SIZE); if (!sdp) return AVERROR(ENOMEM); /* We create the SDP based on the RTSP AVFormatContext where we * aren't allowed to change the filename field. (We create the SDP * based on the RTSP context since the contexts for the RTP streams * don't exist yet.) In order to specify a custom URL with the actual * peer IP instead of the originally specified hostname, we create * a temporary copy of the AVFormatContext, where the custom URL is set. * * FIXME: Create the SDP without copying the AVFormatContext. * This either requires setting up the RTP stream AVFormatContexts * already here (complicating things immensely) or getting a more * flexible SDP creation interface. */ sdp_ctx = *s; sdp_ctx.url = url; ff_url_join(url, sizeof(url), "rtsp", NULL, addr, -1, NULL); ctx_array[0] = &sdp_ctx; if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) { av_free(sdp); return AVERROR_INVALIDDATA; } av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp); ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri, "Content-Type: application/sdp\r\n", reply, NULL, sdp, strlen(sdp)); av_free(sdp); if (reply->status_code != RTSP_STATUS_OK) return ff_rtsp_averror(reply->status_code, AVERROR_INVALIDDATA); /* Set up the RTSPStreams for each AVStream */ for (i = 0; i < s->nb_streams; i++) { RTSPStream *rtsp_st; rtsp_st = av_mallocz(sizeof(RTSPStream)); if (!rtsp_st) return AVERROR(ENOMEM); dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st); rtsp_st->stream_index = i; av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url)); /* Note, this must match the relative uri set in the sdp content */ av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/streamid=%d", i); } return 0; } ``` -------------------------------- ### Main Function Definition Source: https://ffmpeg.org/doxygen/4.4/http__multiclient_8c.html Defines the `main` function, the entry point for the HTTP multi-client example. It handles command-line arguments and initiates the server setup. ```c int main ( int _argc_ , char ** _argv_ ) ``` -------------------------------- ### Main Function Entry Point Source: https://ffmpeg.org/doxygen/4.4/transcoding_8c_source.html Parses command-line arguments, opens input and output files, initializes filters, and starts the main processing loop. Handles argument validation and initial setup. ```c int main(int argc, char **argv) { int ret; AVPacket *packet = NULL; unsigned int stream_index; unsigned int i; if (argc != 3) { av_log(NULL, AV_LOG_ERROR, "Usage: %s \n", argv[0]); return 1; } if ((ret = open_input_file(argv[1])) < 0) goto end; if ((ret = open_output_file(argv[2])) < 0) goto end; if ((ret = init_filters()) < 0) goto end; if (!(packet = av_packet_alloc())) goto end; /* read all packets */ while (1) { if ((ret = av_read_frame(ifmt_ctx, packet)) < 0) break; stream_index = packet->stream_index; av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n", stream_index); if (filter_ctx[stream_index].filter_graph) { StreamContext *stream = &stream_ctx[stream_index]; av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n"); av_packet_rescale_ts(packet, ifmt_ctx->streams[stream_index]->time_base, stream->dec_ctx->time_base); ret = avcodec_send_packet(stream->dec_ctx, packet); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Decoding failed\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(stream->dec_ctx, stream->dec_frame); if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) break; else if (ret < 0) goto end; stream->dec_frame->pts = stream->dec_frame->best_effort_timestamp; ret = filter_encode_write_frame(stream->dec_frame, stream_index); if (ret < 0) goto end; } } else { /* remux this frame without reencoding */ av_packet_rescale_ts(packet, ifmt_ctx->streams[stream_index]->time_base, ofmt_ctx->streams[stream_index]->time_base); ret = av_interleaved_write_frame(ofmt_ctx, packet); if (ret < 0) goto end; } av_packet_unref(packet); } /* flush filters and encoders */ for (i = 0; i < ifmt_ctx->nb_streams; i++) { /* flush filter */ if (!filter_ctx[i].filter_graph) continue; ret = filter_encode_write_frame(NULL, i); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n"); goto end; } /* flush encoder */ ret = flush_encoder(i); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n"); goto end; } } av_write_trailer(ofmt_ctx); end: av_packet_free(&packet); for (i = 0; i < ifmt_ctx->nb_streams; i++) { avcodec_free_context(&stream_ctx[i].dec_ctx); if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx) avcodec_free_context(&stream_ctx[i].enc_ctx); if (filter_ctx && filter_ctx[i].filter_graph) { avfilter_graph_free(&filter_ctx[i].filter_graph); av_packet_free(&filter_ctx[i].enc_pkt); av_frame_free(&filter_ctx[i].filtered_frame); } av_frame_free(&stream_ctx[i].dec_frame); } av_free(filter_ctx); av_free(stream_ctx); avformat_close_input(&ifmt_ctx); if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) avio_closep(&ofmt_ctx->pb); avformat_free_context(ofmt_ctx); if (ret < 0) av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret)); return ret ? 1 : 0; } ``` -------------------------------- ### Setup SRT Connection Source: https://ffmpeg.org/doxygen/4.4/libsrt_8c_source.html Initiates the SRT connection setup after parsing options. Handles potential errors during the setup process. ```c ret = libsrt_setup(h, uri, flags); if (ret < 0) goto err; return 0; ``` -------------------------------- ### setup_window Source: https://ffmpeg.org/doxygen/4.4/xcbgrab_8c_source.html Sets up the XCB window for grabbing. ```APIDOC ## setup_window ### Description This function initializes and configures the XCB window context for screen capturing. ### Signature `static void setup_window(AVFormatContext *s)` ### Parameters - **s** (AVFormatContext *): Pointer to the AVFormatContext. ``` -------------------------------- ### Start GUID Function for W64 Muxer Source: https://ffmpeg.org/doxygen/4.4/wavenc_8c_source.html Writes a GUID and a size field to the AVIOContext, marking the start of a chunk in the W64 format. ```c static void start_guid(AVIOContext *pb, const uint8_t *guid, int64_t *pos) { *pos = avio_tell(pb); avio_write(pb, guid, 16); avio_wl64(pb, INT64_MAX); } ``` -------------------------------- ### Write ASF Header with GUID and Size Source: https://ffmpeg.org/doxygen/4.4/asfenc_8c_source.html Writes an ASF header chunk, starting with a GUID and a placeholder for the size, returning the starting position for later size update. ```c static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g) { int64_t pos; pos = avio_tell(pb); ff_put_guid(pb, g); avio_wl64(pb, 24); return pos; } ``` -------------------------------- ### Get GUID Function Source: https://ffmpeg.org/doxygen/4.4/asfdec__o_8c_source.html Retrieves a Globally Unique Identifier (GUID) from an AVIOContext. ```c int ff_get_guid(AVIOContext *s, ff_asf_guid *g) ``` -------------------------------- ### Example: Opening an H.264 Decoder Source: https://ffmpeg.org/doxygen/4.4/avcodec_8h_source.html Demonstrates how to find an H.264 decoder, allocate a context, and open the codec with specific options. Ensure to handle errors after each step. ```c av_dict_set(&opts, "b", "2.5M", 0); codec = avcodec_find_decoder(AV_CODEC_ID_H264); if (!codec) exit(1); context = avcodec_alloc_context3(codec); if (avcodec_open2(context, codec, opts) < 0) exit(1); ``` -------------------------------- ### setup_window Function Prototype Source: https://ffmpeg.org/doxygen/4.4/xcbgrab_8c_source.html Function to set up the window for XCB grabbing. ```c static void setup_window(AVFormatContext *s) ``` -------------------------------- ### MPEG2 Get Frame Start Source: https://ffmpeg.org/doxygen/4.4/vaapi__mpeg2_8c_source.html Determines if the current position in the MPEG2 stream marks the start of a frame. This is crucial for frame synchronization. ```c static int mpeg2_get_is_frame_start(const MpegEncContext *s) { return mpeg2_get_is_frame_start(s); } ``` -------------------------------- ### FFmpeg Initialization and Setup Source: https://ffmpeg.org/doxygen/4.4/hw__decode_8c.html Includes necessary headers for FFmpeg libraries and common utilities. This setup is typical for applications interacting with FFmpeg for media processing. ```c #include #include #include #include #include #include #include #include ``` -------------------------------- ### Main Function: AVIOContext Reading Example Source: https://ffmpeg.org/doxygen/4.4/avio__reading_8c_source.html This is the main entry point for the AVIOContext reading example. It handles command-line arguments, maps the input file into memory, sets up the AVIOContext with a custom read callback, and then opens the input format. Error handling and resource cleanup are included. ```c int main(int argc, char *argv[]) { AVFormatContext *fmt_ctx = NULL; AVIOContext *avio_ctx = NULL; uint8_t *buffer = NULL, *avio_ctx_buffer = NULL; size_t buffer_size, avio_ctx_buffer_size = 4096; char *input_filename = NULL; int ret = 0; struct buffer_data bd = { 0 }; if (argc != 2) { fprintf(stderr, "usage: %s input_file\n" "API example program to show how to read from a custom buffer " "accessed through AVIOContext.\n", argv[0]); return 1; } input_filename = argv[1]; /* slurp file content into buffer */ ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL); if (ret < 0) goto end; /* fill opaque structure used by the AVIOContext read callback */ bd.ptr = buffer; bd.size = buffer_size; if (!(fmt_ctx = avformat_alloc_context())) { ret = AVERROR(ENOMEM); goto end; } avio_ctx_buffer = av_malloc(avio_ctx_buffer_size); if (!avio_ctx_buffer) { ret = AVERROR(ENOMEM); goto end; } avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, &read_packet, NULL, NULL); if (!avio_ctx) { ret = AVERROR(ENOMEM); goto end; } fmt_ctx->pb = avio_ctx; ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL); if (ret < 0) { fprintf(stderr, "Could not open input\n"); goto end; } ret = avformat_find_stream_info(fmt_ctx, NULL); if (ret < 0) { fprintf(stderr, "Could not find stream information\n"); goto end; } av_dump_format(fmt_ctx, 0, input_filename, 0); end: avformat_close_input(&fmt_ctx); /* note: the internal buffer could have changed, and be != avio_ctx_buffer */ if (avio_ctx) av_freep(&avio_ctx->buffer); avio_context_free(&avio_ctx); av_file_unmap(buffer, buffer_size); if (ret < 0) { fprintf(stderr, "Error occurred: %s\n", av_err2str(ret)); return 1; } return 0; } ``` -------------------------------- ### Get Starting Slice Offset Source: https://ffmpeg.org/doxygen/4.4/rv34_8c_source.html Determines the starting slice offset based on the macroblock size. This function is potentially replaceable with `ff_h263_decode_mba()`. ```c int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size) { int i; for(i = 0; i < 5; i++) if(rv34_mb_max_sizes[i] >= mb_size - 1) break; return rv34_mb_bits_sizes[i]; } ``` -------------------------------- ### Compile qt-faststart Utility Source: https://ffmpeg.org/doxygen/4.4/qt-faststart_8c_source.html Instructions for compiling the qt-faststart utility from the FFmpeg source directory. Ensure you compile from the base directory for correct results, especially on 64-bit systems. ```bash make tools/qt-faststart ``` -------------------------------- ### RV34 Get Start Offset Function Source: https://ffmpeg.org/doxygen/4.4/rv30_8c_source.html Retrieves the starting offset for macroblocks in RV34 decoding. It uses a GetBitContext for bitstream reading. ```c int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size) { // Decode starting slice position. return get_interleaved_ue_golomb(gb); } ``` -------------------------------- ### VC1 VDPAU Start Frame Function Source: https://ffmpeg.org/doxygen/4.4/vdpau__vc1_8c.html Initializes the start of a VC1 frame for VDPAU hardware acceleration. This function is part of the hardware acceleration setup. ```c static int vdpau_vc1_start_frame (AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) ``` -------------------------------- ### Main Function for Video Scaling Example Source: https://ffmpeg.org/doxygen/4.4/scaling__video_8c.html This is the main entry point for the video scaling example. It sets up the necessary structures and calls other functions to perform scaling. ```c int main(int argc, char **argv) { static int sws_flags = SWS_BICUBIC; img_convert_ctx = sws_getContext(width, height, pix_fmt, width, height, AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL); if (!img_convert_ctx) { fprintf(stderr, "Could not initialize the conversion context\n"); return 1; } /* fill the image */ fill_yuv_image(data, linesize, width, height, frame_idx); /* convert the image to the desired format */ sws_scale(img_convert_ctx, (const uint8_t *const *)data, linesize, 0, height, data_converted, linesize_converted); /* do something with the converted image */ sws_freeContext(img_convert_ctx); return 0; } ``` -------------------------------- ### Setup Find Stream Info Options Source: https://ffmpeg.org/doxygen/4.4/cmdutils_8c_source.html Sets up AVDictionary options for avformat_find_stream_info(). ```c AVDictionary ** setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts) { // Implementation details omitted for brevity return NULL; // Placeholder } ``` -------------------------------- ### DXVA Get Decoder GUID Function Source: https://ffmpeg.org/doxygen/4.4/dxva2_8c_source.html Selects the appropriate DXVA decoder GUID based on codec compatibility and output format validation. It iterates through available DXVA modes and checks against supported GUIDs and formats. ```c static int dxva_get_decoder_guid(AVCodecContext *avctx, void *service, void *surface_format, unsigned guid_count, const GUID *guid_list, GUID *decoder_guid) { FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx); unsigned i, j; dxva_list_guids_debug(avctx, service, guid_count, guid_list); *decoder_guid = ff_GUID_NULL; for (i = 0; dxva_modes[i].guid; i++) { const dxva_mode *mode = &dxva_modes[i]; int validate; if (!dxva_check_codec_compatibility(avctx, mode)) continue; for (j = 0; j < guid_count; j++) { if (IsEqualGUID(mode->guid, &guid_list[j])) break; } if (j == guid_count) continue; #if CONFIG_D3D11VA if (sctx->pix_fmt == AV_PIX_FMT_D3D11) validate = d3d11va_validate_output(service, *mode->guid, surface_format); #endif #if CONFIG_DXVA2 if (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) validate = dxva2_validate_output(service, *mode->guid, surface_format); #endif if (validate) { *decoder_guid = *mode->guid; break; } } if (IsEqualGUID(decoder_guid, &ff_GUID_NULL)) { av_log(avctx, AV_LOG_VERBOSE, "No decoder device for codec found\n"); return AVERROR(EINVAL); } if (IsEqualGUID(decoder_guid, &ff_DXVADDI_Intel_ModeH264_E)) sctx->workaround |= FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO; return 0; } ``` -------------------------------- ### Invoke qt-faststart Utility Source: https://ffmpeg.org/doxygen/4.4/qt-faststart_8c_source.html How to run the qt-faststart utility to rearrange QuickTime files for network streaming. The utility moves the 'moov' atom to the beginning of the file. ```bash qt-faststart ``` -------------------------------- ### Get Codec GUID from AVCodecID Source: https://ffmpeg.org/doxygen/4.4/riffenc_8c_source.html Retrieves the FF ASF GUID for a given AVCodecID from a provided array of AVCodecGuid structures. Returns NULL if the codec ID is not found. ```c const ff_asf_guid *ff_get_codec_guid(enum AVCodecID id, const AVCodecGuid *av_guid) { int i; for (i = 0; av_guid[i].id != AV_CODEC_ID_NONE; i++) { if (id == av_guid[i].id) return &(av_guid[i].guid); } return NULL; } ``` -------------------------------- ### Codec Control Source: https://ffmpeg.org/doxygen/4.4/mediacodec__wrapper_8h_source.html Functions to start, stop, flush, and get information about MediaCodec instances. ```APIDOC ## ff_AMediaCodec_start ### Description Starts the MediaCodec instance. ### Signature ```c int ff_AMediaCodec_start(FFAMediaCodec *codec) ``` ## ff_AMediaCodec_flush ### Description Flushes the MediaCodec instance, discarding all pending input and output data. ### Signature ```c int ff_AMediaCodec_flush(FFAMediaCodec *codec) ``` ## ff_AMediaCodec_getName ### Description Retrieves the name of the MediaCodec instance. ### Signature ```c char * ff_AMediaCodec_getName(FFAMediaCodec *codec) ``` ``` -------------------------------- ### Initialize MPEG-4 Partitions Source: https://ffmpeg.org/doxygen/4.4/mpeg4videoenc_8c.html Sets up the partition structures for MPEG-4 encoding. ```c void ff_mpeg4_init_partitions (MpegEncContext *s) ``` -------------------------------- ### Main entry point for avio_list_dir example Source: https://ffmpeg.org/doxygen/4.4/avio__list__dir_8c.html Parses command-line arguments and initiates directory listing operations. Handles program exit and usage display. ```c #include #include #include int main(int argc, char *argv[]) { if (argc != 2) { usage(argv[0]); } if (list_op(argv[1]) < 0) { return 1; } return 0; } ``` -------------------------------- ### Get Relative Time (Microseconds) Source: https://ffmpeg.org/doxygen/4.4/time_8c_source.html Gets the current time in microseconds since an unspecified starting point. It prioritizes `clock_gettime` with `CLOCK_MONOTONIC` if available, otherwise it falls back to `av_gettime` with an offset. ```c #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC) #ifdef __APPLE__ if (&clock_gettime) #endif { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000; } #endif return av_gettime() + 42 * 60 * 60 * INT64_C(1000000); ``` -------------------------------- ### Encode Init Source: https://ffmpeg.org/doxygen/4.4/mpeg12_8h_source.html Initializes the MPEG-1 encoder context. This setup is required before starting the encoding process. ```c void ff_mpeg1_encode_init(MpegEncContext *s); ``` -------------------------------- ### Main Function Example Source: https://ffmpeg.org/doxygen/4.4/tests_2cast5_8c.html Entry point for the cast5 test application. It requires command-line arguments for execution. ```c #include "libavutil/cast5.h" #include "libavutil/log.h" int main (int _argc_ , char ** _argv_ ) ``` -------------------------------- ### Setup Stream Info Options Source: https://ffmpeg.org/doxygen/4.4/cmdutils_8c_source.html Allocates and populates an array of dictionaries, where each dictionary contains codec options filtered for a specific stream. Requires a format context and base codec options. ```c AVDictionary **setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts) { int i; AVDictionary **opts; if (!s->nb_streams) return NULL; opts = av_mallocz_array(s->nb_streams, sizeof(*opts)); if (!opts) { av_log(NULL, AV_LOG_ERROR, "Could not alloc memory for stream options.\n"); return NULL; } for (i = 0; i < s->nb_streams; i++) opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id, s, s->streams[i], NULL); return opts; } ``` -------------------------------- ### Get Current Time Source: https://ffmpeg.org/doxygen/4.4/fft_8c_source.html Retrieves the current time in microseconds relative to an unspecified starting point. ```c int64_t av_gettime_relative(void) Get the current time in microseconds since some unspecified starting point. **Definition:** time.c:56 ``` -------------------------------- ### Setup Quantization Tables Source: https://ffmpeg.org/doxygen/4.4/mobiclip_8c_source.html Sets up the quantization tables for the codec. ```c static int setup_qtables(AVCodecContext *avctx, int quantizer) { int i, ret; static const uint8_t block4x4_coefficients_tab[] = { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15, 16, 17, 20, 21, 18, 19, 22, 23, 24, 25, 28, 29, 26, 27, 30, 31, 32, 33, 36, 37, 34, 35, 38, 39, 40, 41, 44, 45, 42, 43, 46, 47, 48, 49, 52, 53, 50, 51, 54, 55, 56, 57, 60, 61, 58, 59, 62, 63 }; static const uint8_t zigzag4x4_tab[] = { 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 15, 14, 11, 7, 10, 13 }; const uint8_t *coeffs = block4x4_coefficients_tab; const uint8_t *zigzag = zigzag4x4_tab; for (i = 0; i < 64; i++) { int coeff_idx = coeffs[i]; int q_idx = zigzag[i]; if (q_idx < 16) { ret = setup_qtable(avctx, quantizer, q_idx, coeff_idx); if (ret < 0) return ret; } } return 0; } ``` -------------------------------- ### Get MPEG2 f_code Source: https://ffmpeg.org/doxygen/4.4/vaapi__mpeg2_8c_source.html Helper function to retrieve the f_code values for MPEG2, used in frame parameter setup. ```c static inline int mpeg2_get_f_code(const MpegEncContext *s) { return (s->mpeg_f_code[0][0] << 12) | (s->mpeg_f_code[0][1] << 8) | (s->mpeg_f_code[1][0] << 4) | s->mpeg_f_code[1][1]; } ``` -------------------------------- ### Include Example Tables Header (Alternative) Source: https://ffmpeg.org/doxygen/4.4/tablegen_8txt.html Includes an alternative or local example tables header file. ```c #include "example_tables.h" ``` -------------------------------- ### Setup RTSP HTTP Tunneling (GET Request) Source: https://ffmpeg.org/doxygen/4.4/rtsp_8c_source.html Initializes the RTSP control connection for HTTP tunneling using a GET request. Sets up headers, including a session cookie and accept types, and connects to the specified HTTP/HTTPS URL. ```c char httpname[1024]; char sessioncookie[17]; char headers[1024]; AVDictionary *options = NULL; av_dict_set_int(&options, "timeout", rt->stimeout, 0); ff_url_join(httpname, sizeof(httpname), https_tunnel ? "https" : "http", auth, host, port, "%s", path); snprintf(sessioncookie, sizeof(sessioncookie), "%08x%08x", av_get_random_seed(), av_get_random_seed()); /* GET requests */ if (ffurl_alloc(&rt->rtsp_hd, httpname, AVIO_FLAG_READ, &s->interrupt_callback) < 0) { err = AVERROR(EIO); goto fail; } /* generate GET headers */ snprintf(headers, sizeof(headers), "x-sessioncookie: %s\r\n" "Accept: application/x-rtsp-tunnelled\r\n" "Pragma: no-cache\r\n" "Cache-Control: no-cache\r\n", sessioncookie); av_opt_set(rt->rtsp_hd->priv_data, "headers", headers, 0); if (!rt->rtsp_hd->protocol_whitelist && s->protocol_whitelist) { rt->rtsp_hd->protocol_whitelist = av_strdup(s->protocol_whitelist); if (!rt->rtsp_hd->protocol_whitelist) { err = AVERROR(ENOMEM); goto fail; } } /* complete the connection */ if (ffurl_connect(rt->rtsp_hd, &options)) { av_dict_free(&options); err = AVERROR(EIO); goto fail; } ``` -------------------------------- ### Main Program Entry Point Source: https://ffmpeg.org/doxygen/4.4/videogen_8c_source.html Parses command-line arguments, initializes video parameters, and generates video frames. It handles output to stdout or individual PGM files. ```c int main(int argc, char **argv) { int w, h, i; char buf[1024]; int isdir = 0; if (argc < 2 || argc > 4) { print_help(argv[0]); } if (!freopen(argv[1], "wb", stdout)) isdir = 1; w = DEFAULT_WIDTH; if(argc > 2) { w = atoi(argv[2]); if (w < 1) print_help(argv[0]); } h = DEFAULT_HEIGHT; if(argc > 3) { h = atoi(argv[3]); if (h < 1) print_help(argv[0]); } rgb_tab = malloc(w * h * 3); wrap = w * 3; width = w; height = h; for (i = 0; i < DEFAULT_NB_PICT; i++) { gen_image(i, w, h); if (isdir) { snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i); pgmyuv_save(buf, w, h, rgb_tab); } else { pgmyuv_save(NULL, w, h, rgb_tab); } } free(rgb_tab); return 0; } ``` -------------------------------- ### Get Tree Decision Source: https://ffmpeg.org/doxygen/4.4/vp56_8h_source.html Decodes a value from a decision tree based on probabilities. The tree structure guides the decoding process. ```c static av_always_inline int vp56_rac_get_tree(VP56RangeCoder *c, const VP56Tree *tree, const uint8_t *probs) { while (tree->val > 0) { if (vp56_rac_get_prob_branchy(c, probs[tree->prob_idx])) tree += tree->val; else tree++; } return -tree->val; } ``` -------------------------------- ### Get OpenCL Event Execution Time Source: https://ffmpeg.org/doxygen/4.4/opencl_8h_source.html Retrieves the command start and end times for a given OpenCL event and calculates the duration. ```c cl_ulong ff_opencl_get_event_time(cl_event event); ``` -------------------------------- ### Usage Example Source: https://ffmpeg.org/doxygen/4.4/sidxindex_8c_source.html Displays the correct command-line usage for the tool. This is typically shown when incorrect arguments are provided. ```c #include #include #include "libavformat/avformat.h" #include "libavutil/avstring.h" #include "libavutil/intreadwrite.h" #include "libavutil/mathematics.h" static int usage(const char *argv0, int ret) { fprintf(stderr, "%s -out foo.mpd file1\n", argv0); return ret; } ``` -------------------------------- ### Basic Test Setup Source: https://ffmpeg.org/doxygen/4.4/videogen_8c.html This C code snippet includes standard libraries and a utility file, suitable for setting up basic tests within the FFmpeg project. ```c #include #include #include #include "utils.c" ``` -------------------------------- ### Get Current Position in PutByteContext Source: https://ffmpeg.org/doxygen/4.4/bytestream_8h_source.html Returns the current write offset from the start of the buffer in a PutByteContext. Useful for tracking how much has been written. ```c static av_always_inline int bytestream2_tell_p(PutByteContext *p) { return (int)(p->buffer - p->buffer_start); } ``` -------------------------------- ### Main Function Example Source: https://ffmpeg.org/doxygen/4.4/tests_2drawutils_8c.html Entry point for the drawutils test program. Includes necessary headers for pixel description and drawing utilities. ```c #include #include "libavutil/pixdesc.h" #include "libavfilter/drawutils.h" int main (void) ``` -------------------------------- ### Get Total Buffer Size for PutByteContext Source: https://ffmpeg.org/doxygen/4.4/bytestream_8h_source.html Returns the total size of the buffer managed by a PutByteContext. This is the difference between the buffer end and start pointers. ```c static av_always_inline int bytestream2_size_p(PutByteContext *p) { return (int)(p->buffer_end - p->buffer_start); } ``` -------------------------------- ### QSV Decoding Example Source: https://ffmpeg.org/doxygen/4.4/examples.html Illustrates hardware-accelerated video decoding using Intel Quick Sync Video (QSV) with FFmpeg. ```c // qsvdec.c // This file demonstrates video decoding using Intel's Quick Sync Video (QSV) hardware acceleration. // It requires specific hardware and FFmpeg build configurations. ``` -------------------------------- ### Get Total Buffer Size for GetByteContext Source: https://ffmpeg.org/doxygen/4.4/bytestream_8h_source.html Returns the total size of the buffer managed by a GetByteContext. This is the difference between the buffer end and start pointers. ```c static av_always_inline int bytestream2_size(GetByteContext *g) { return (int)(g->buffer_end - g->buffer_start); } ``` -------------------------------- ### ff_vp7dsp_init Source: https://ffmpeg.org/doxygen/4.4/vp8dsp_8h.html Initializes the VP7 DSP context. ```APIDOC ## Function: ff_vp7dsp_init ### Description Initializes the VP7 DSP context. ### Signature `void ff_vp7dsp_init(VP8DSPContext *_c)` ### Parameters - `_c` (VP8DSPContext *) - Pointer to the VP8DSPContext to initialize. ``` -------------------------------- ### Get Packed Sample Format Source: https://ffmpeg.org/doxygen/4.4/audio__convert_8c_source.html Returns the packed alternative for a given sample format. For example, it converts planar formats to their packed equivalents. ```c enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt) ``` -------------------------------- ### ff_vp78dsp_init Source: https://ffmpeg.org/doxygen/4.4/vp8dsp_8h.html Initializes the VP7/VP8 DSP context. ```APIDOC ## Function: ff_vp78dsp_init ### Description Initializes the VP7/VP8 DSP context. ### Signature `void ff_vp78dsp_init(VP8DSPContext *_c)` ### Parameters - `_c` (VP8DSPContext *) - Pointer to the VP8DSPContext to initialize. ``` -------------------------------- ### Vulkan Frame Processing Setup Source: https://ffmpeg.org/doxygen/4.4/vf__scale__vulkan_8c_source.html Starts Vulkan execution recording and retrieves the command buffer. It then creates image views for input and output frames. ```c ff_vk_start_exec_recording(avctx, s->exec); cmd_buf = ff_vk_get_exec_buf(avctx, s->exec); for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.input_format); i++) { RET(ff_vk_create_imageview(avctx, s->exec, &s->input_images[i].imageView, in->img[i], av_vkfmt_from_pixfmt(s->vkctx.input_format)[i], ff_comp_identity_map)); s->input_images[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; } for (int i = 0; i < av_pix_fmt_count_planes(s->vkctx.output_format); i++) { RET(ff_vk_create_imageview(avctx, s->exec, &s->output_images[i].imageView, out->img[i], av_vkfmt_from_pixfmt(s->vkctx.output_format)[i], ff_comp_identity_map)); s->output_images[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; } ff_vk_update_descriptor_set(avctx, s->pl, 0); ``` -------------------------------- ### Main Function for Video Encoding Source: https://ffmpeg.org/doxygen/4.4/encode__video_8c_source.html This is the main entry point for the video encoding example. It parses command-line arguments for the output file and codec name, initializes the codec context, allocates necessary structures, sets encoding parameters, opens the codec, and then proceeds to encode a series of frames. ```c int main(int argc, char **argv) { const char *filename, *codec_name; const AVCodec *codec; AVCodecContext *c= NULL; int i, ret, x, y; FILE *f; AVFrame *frame; AVPacket *pkt; uint8_t endcode[] = { 0, 0, 1, 0xb7 }; if (argc <= 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(0); } filename = argv[1]; codec_name = argv[2]; /* find the mpeg1video encoder */ codec = avcodec_find_encoder_by_name(codec_name); if (!codec) { fprintf(stderr, "Codec '%s' not found\n", codec_name); exit(1); } c = avcodec_alloc_context3(codec); if (!c) { fprintf(stderr, "Could not allocate video codec context\n"); exit(1); } pkt = av_packet_alloc(); if (!pkt) exit(1); /* put sample parameters */ c->bit_rate = 400000; /* resolution must be a multiple of two */ c->width = 352; c->height = 288; /* frames per second */ c->time_base = (AVRational){1, 25}; c->framerate = (AVRational){25, 1}; /* emit one intra frame every ten frames * check frame pict_type before passing frame * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I * then gop_size is ignored and the output of encoder * will always be I frame irrespective to gop_size */ c->gop_size = 10; c->max_b_frames = 1; c->pix_fmt = AV_PIX_FMT_YUV420P; if (codec->id == AV_CODEC_ID_H264) av_opt_set(c->priv_data, "preset", "slow", 0); /* open it */ ret = avcodec_open2(c, codec, NULL); if (ret < 0) { fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret)); exit(1); } f = fopen(filename, "wb"); if (!f) { fprintf(stderr, "Could not open %s\n", filename); exit(1); } frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate video frame\n"); exit(1); } frame->format = c->pix_fmt; frame->width = c->width; frame->height = c->height; ret = av_frame_get_buffer(frame, 0); if (ret < 0) { fprintf(stderr, "Could not allocate the video frame data\n"); exit(1); } /* encode 1 second of video */ for (i = 0; i < 25; i++) { fflush(stdout); /* make sure the frame data is writable */ ret = av_frame_make_writable(frame); if (ret < 0) exit(1); /* prepare a dummy image */ /* Y */ for (y = 0; y < c->height; y++) { for (x = 0; x < c->width; x++) { frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3; } } /* Cb and Cr */ for (y = 0; y < c->height/2; y++) { for (x = 0; x < c->width/2; x++) { frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2; frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5; } } frame->pts = i; /* encode the image */ encode(c, frame, pkt, f); } /* flush the encoder */ encode(c, NULL, pkt, f); /* add sequence end code to have a real MPEG file */ if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO) fwrite(endcode, 1, sizeof(endcode), f); fclose(f); avcodec_free_context(&c); av_frame_free(&frame); av_packet_free(&pkt); return 0; } ``` -------------------------------- ### Frame Processing Initialization Source: https://ffmpeg.org/doxygen/4.4/vf__datascope_8c_source.html Initializes frame processing by getting an output buffer and setting up thread data. This is a setup function before detailed frame analysis. ```c static int filter_frame(AVFilterLink *inlink, AVFrame *in) { AVFilterContext *ctx = inlink->dst; DatascopeContext *s = ctx->priv; AVFilterLink *outlink = ctx->outputs[0]; const int P = FFMAX(s->nb_planes, s->nb_comps); ThreadData td = { 0 }; int ymaxlen = 0; int xmaxlen = 0; int PP = 0; AVFrame *out; out = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out) { ``` -------------------------------- ### Get Codec ID from GUIDs Source: https://ffmpeg.org/doxygen/4.4/riffdec_8c_source.html Maps a given ff_asf_guid to an AVCodecID using a provided array of AVCodecGuid structures. Returns AV_CODEC_ID_NONE if no match is found. ```c enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid) { int i; for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++) if (!ff_guidcmp(guids[i].guid, guid)) return guids[i].id; return AV_CODEC_ID_NONE; } ``` -------------------------------- ### Main function for AVIOContext example Source: https://ffmpeg.org/doxygen/4.4/avio_reading_8c-example.html Sets up the AVIOContext with a custom read callback and opens an input file. It maps the file into memory, allocates buffers, and configures the AVFormatContext to use the custom IO. Ensure to free allocated resources, including the AVIOContext buffer, in the end. ```c int main(int argc, char *argv[]) { AVFormatContext *fmt_ctx = NULL; AVIOContext *avio_ctx = NULL; uint8_t *buffer = NULL, *avio_ctx_buffer = NULL; size_t buffer_size, avio_ctx_buffer_size = 4096; char *input_filename = NULL; int ret = 0; struct buffer_data bd = { 0 }; if (argc != 2) { fprintf(stderr, "usage: %s input_file\n" "API example program to show how to read from a custom buffer " "accessed through AVIOContext.\n", argv[0]); return 1; } input_filename = argv[1]; /* slurp file content into buffer */ ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL); if (ret < 0) goto end; /* fill opaque structure used by the AVIOContext read callback */ bd.ptr = buffer; bd.size = buffer_size; if (!(fmt_ctx = avformat_alloc_context())) { ret = AVERROR(ENOMEM); goto end; } avio_ctx_buffer = av_malloc(avio_ctx_buffer_size); if (!avio_ctx_buffer) { ret = AVERROR(ENOMEM); goto end; } avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, &read_packet, NULL, NULL); if (!avio_ctx) { ret = AVERROR(ENOMEM); goto end; } fmt_ctx->pb = avio_ctx; ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL); if (ret < 0) { fprintf(stderr, "Could not open input\n"); goto end; } ret = avformat_find_stream_info(fmt_ctx, NULL); if (ret < 0) { fprintf(stderr, "Could not find stream information\n"); goto end; } av_dump_format(fmt_ctx, 0, input_filename, 0); end: avformat_close_input(&fmt_ctx); /* note: the internal buffer could have changed, and be != avio_ctx_buffer */ if (avio_ctx) av_freep(&avio_ctx->buffer); avio_context_free(&avio_ctx); av_file_unmap(buffer, buffer_size); if (ret < 0) { fprintf(stderr, "Error occurred: %s\n", av_err2str(ret)); return 1; } return 0; } ``` -------------------------------- ### Get MPEG-TS Payload Start Pointer Source: https://ffmpeg.org/doxygen/4.4/mpegtsenc_8c_source.html Returns a pointer to the beginning of the payload section of an MPEG-TS packet. It accounts for the presence and length of an adaptation field. ```c static uint8_t *get_ts_payload_start(uint8_t *pkt) { if (pkt[3] & 0x20) return pkt + 5 + pkt[4]; else return pkt + 4; } ``` -------------------------------- ### Example: FFmpeg Logging in Main Function Source: https://ffmpeg.org/doxygen/4.4/tests_2log_8c_source.html Demonstrates setting the log level to debug and then logging messages at various levels within a loop. It also includes tests for `av_log_format_line2` with different buffer sizes. ```c int main(int argc, char **argv) { int i; av_log_set_level(AV_LOG_DEBUG); for (use_color=0; use_color<=256; use_color = 255*use_color+1) { av_log(NULL, AV_LOG_FATAL, "use_color: %d\n", use_color); for (i = AV_LOG_DEBUG; i>=AV_LOG_QUIET; i-=8) { av_log(NULL, i, " %d", i); av_log(NULL, AV_LOG_INFO, "e "); av_log(NULL, i + 256*123, "C%d", i); av_log(NULL, AV_LOG_INFO, "e"); } av_log(NULL, AV_LOG_PANIC, "\n"); } { int result; char buffer[4]; result = call_log_format_line2("foo", NULL, 0); if(result != 3) { printf("Test NULL buffer failed.\n"); return 1; } result = call_log_format_line2("foo", buffer, 2); if(result != 3 || strncmp(buffer, "f", 2)) { printf("Test buffer too small failed.\n"); return 1; } result = call_log_format_line2("foo", buffer, 4); if(result != 3 || strncmp(buffer, "foo", 4)) { printf("Test buffer sufficiently big failed.\n"); return 1; } } return 0; } ``` -------------------------------- ### init() Function Source: https://ffmpeg.org/doxygen/4.4/pcm__rechunk__bsf_8c.html Initializes the AVBSFContext for PCM rechunking. ```c static int init (AVBSFContext * _ctx_) ``` -------------------------------- ### Get UTF-8 Length Source: https://ffmpeg.org/doxygen/4.4/movtextdec_8c_source.html Calculates the byte length of a UTF-8 encoded character sequence starting at the given text pointer. Returns 0 on error. ```c // Return byte length of the UTF-8 sequence starting at text[0]. 0 on error. static int get_utf8_length_at(const char *text, const char *text_end) { const char *start = text; int err = 0; uint32_t c; GET_UTF8(c, text < text_end ? (uint8_t)*text++ : (err = 1, 0), goto error;); if (err) goto error; return text - start; error: return 0; } ```