1 STATIC
void ctx_lock(void)
5 r
= pthread_mutex_lock(&ctx_mutex_l
);
7 fatal("%d:unable to lock the format context\n", r
);
9 STATIC
void ctx_unlock(void)
13 r
= pthread_mutex_unlock(&ctx_mutex_l
);
15 fatal("%d:unable to unlock the format context\n", r
);
17 STATIC u8
*duration_estimate_to_str(enum avformat_duration_estimation_method_t
21 case avformat_duration_from_pts
:
22 return "from PTS(Presentation TimeStamp)";
23 case avformat_duration_from_st
:
25 case avformat_duration_from_bitrate
:
26 return "from bitrate";
32 #define LIMITS_REACHED 1
35 * we don't want to lock any pkt q for too long, then we do finer-grained
38 STATIC u8
pkts_read_and_q(void) { loop
42 if (did_reached_limits())
43 return LIMITS_REACHED
;
44 /* XXX: there, new streams can appear (could some disappear?) */
46 r
= avformat_read_pkt(ctx_p
, pkt_l
);
48 if (r
== AVERROR(EAGAIN
))
50 else if (r
== AVERROR_EOF
)
53 fatal("ffmpeg:error while reading coded/compressed data into packets\n");
55 if (pkt_l
->st_idx
== npv_audio_st_p
.idx
) {
56 npv_pipeline_limits_lock();
57 npv_pipeline_limits_p
.pkts
.audio_bytes_n
+= (u64
)pkt_l
->sz
;
58 if (npv_pipeline_limits_p
.pkts
.prefill
.audio_bytes_rem
> 0)
59 npv_pipeline_limits_p
.pkts
.prefill
.audio_bytes_rem
-=
61 npv_pipeline_limits_unlock();
63 npv_pkt_q_lock(npv_audio_pkt_q_p
);
64 /* will "steal" the pkt */
65 npv_pkt_q_enq(npv_audio_pkt_q_p
, pkt_l
);
66 npv_pkt_q_unlock(npv_audio_pkt_q_p
);
67 } else if (pkt_l
->st_idx
== npv_video_st_p
.idx
) {
68 npv_pipeline_limits_lock();
69 npv_pipeline_limits_p
.pkts
.video_bytes_n
+= (u64
)pkt_l
->sz
;
70 if (npv_pipeline_limits_p
.pkts
.prefill
.video_bytes_rem
> 0)
71 npv_pipeline_limits_p
.pkts
.prefill
.video_bytes_rem
-=
73 npv_pipeline_limits_unlock();
75 npv_pkt_q_lock(npv_video_pkt_q_p
);
76 /* will "steal" the pkt */
77 npv_pkt_q_enq(npv_video_pkt_q_p
, pkt_l
);
78 npv_pkt_q_unlock(npv_video_pkt_q_p
);
79 } else /* data, subtitles, non active sts, etc */
80 avcodec_pkt_unref(pkt_l
);
85 STATIC
void probe_best_sts(
86 int *best_v_idx
, int *best_v_id
, avutil_rational_t
**best_v_tb
,
87 int64_t *best_v_start_time
, avcodec_params_t
**best_v_codec_params
,
88 int *best_a_idx
, int *best_a_id
, avutil_rational_t
**best_a_tb
,
89 int64_t *best_a_start_time
, avcodec_params_t
**best_a_codec_params
)
93 /* probe beyond the header, if any */
94 r
= avformat_find_st_info(ctx_p
, 0);
96 fatal("ffmpeg:unable to probe\n");
97 r
= avformat_find_best_st(ctx_p
, AVUTIL_AVMEDIA_TYPE_AUDIO
, -1, -1, 0,
100 fatal("ffmpeg:no audio stream found\n");
101 /* we copy what we need */
103 *best_a_id
= ctx_p
->sts
[r
]->id
;
104 *best_a_tb
= &ctx_p
->sts
[r
]->tb
;
105 *best_a_start_time
= ctx_p
->sts
[r
]->start_time
;
106 *best_a_codec_params
= ctx_p
->sts
[r
]->codecpar
; /* used once for init */
108 r
= avformat_find_best_st(ctx_p
, AVUTIL_AVMEDIA_TYPE_VIDEO
, -1, -1, 0,
111 fatal("ffmpeg:no video stream found\n");
112 /* we copy what we need */
114 *best_v_id
= ctx_p
->sts
[r
]->id
;
115 *best_v_tb
= &ctx_p
->sts
[r
]->tb
;
116 *best_v_start_time
= ctx_p
->sts
[r
]->start_time
;
117 *best_v_codec_params
= ctx_p
->sts
[r
]->codecpar
; /* used once for init */
118 pout("####%s#### probing: %u streams and %u programs\n", ctx_p
->url
, ctx_p
->sts_n
, ctx_p
->programs_n
);
119 pout("####%s#### probing: best video stream index=%d/id=%d, best audio stream index=%d/id=%d\n", ctx_p
->url
, *best_v_idx
, *best_v_id
, *best_a_idx
, *best_a_id
);
121 STATIC
void flush(void)
123 avformat_flush(ctx_p
);
124 avio_flush(ctx_p
->pb
);
126 STATIC
void init_once(u8
*url
)
128 init_once_public(url
);