npv:_reasonable_ "pedanticage" of the code
[nyanmp.git] / npv / fmt / public / code.frag.c
blobf5c1a3873f58959f61357b7e1fd9ef27c2ba4c1a
1 STATIC void ctx_lock(void)
3 int r;
5 r = pthread_mutex_lock(&ctx_mutex_l);
6 if (r != 0)
7 fatal("%d:unable to lock the format context\n", r);
9 STATIC void ctx_unlock(void)
11 int r;
13 r = pthread_mutex_unlock(&ctx_mutex_l);
14 if (r != 0)
15 fatal("%d:unable to unlock the format context\n", r);
17 STATIC u8 *duration_estimate_to_str(enum avformat_duration_estimation_method_t
20 switch (m) {
21 case avformat_duration_from_pts:
22 return "from PTS(Presentation TimeStamp)";
23 case avformat_duration_from_st:
24 return "from stream";
25 case avformat_duration_from_bitrate:
26 return "from bitrate";
27 default:
28 return "unkwown";
31 #define AGAIN 0
32 #define LIMITS_REACHED 1
33 #define EOF_FMT 2
35 * we don't want to lock any pkt q for too long, then we do finer-grained
36 * locking here
38 STATIC u8 pkts_read_and_q(void) { loop
40 int r;
42 if (did_reached_limits())
43 return LIMITS_REACHED;
44 /* XXX: there, new streams can appear (could some disappear?) */
45 ctx_lock();
46 r = avformat_read_pkt(ctx_p, pkt_l);
47 ctx_unlock();
48 if (r == AVERROR(EAGAIN))
49 return AGAIN;
50 else if (r == AVERROR_EOF)
51 return EOF_FMT;
52 else if (r != 0)
53 fatal("ffmpeg:error while reading coded/compressed data into packets\n");
54 /* r == 0 */
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 -=
60 (s64)pkt_l->sz;
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 -=
72 (s64)pkt_l->sz;
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);
82 #undef AGAIN
83 #undef LIMITS_REACHED
84 #undef EOF_FMT
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)
91 int r;
93 /* probe beyond the header, if any */
94 r = avformat_find_st_info(ctx_p, 0);
95 if (r < 0)
96 fatal("ffmpeg:unable to probe\n");
97 r = avformat_find_best_st(ctx_p, AVUTIL_AVMEDIA_TYPE_AUDIO, -1, -1, 0,
98 0);
99 if (r < 0)
100 fatal("ffmpeg:no audio stream found\n");
101 /* we copy what we need */
102 *best_a_idx = r;
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,
110 if (r < 0)
111 fatal("ffmpeg:no video stream found\n");
112 /* we copy what we need */
113 *best_v_idx = r;
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);
129 init_once_local();