]> git.sesse.net Git - nageru/blobdiff - futatabi/main.cpp
Stop using av_init_packet().
[nageru] / futatabi / main.cpp
index bd18780038cade997c40b674b79d3c7ebf83fdf6..00de13f5bc8002cd6fa36456cdaa9925e678c6bf 100644 (file)
@@ -526,44 +526,41 @@ void record_thread_func()
                vector<uint32_t> pending_audio[MAX_STREAMS];
                int64_t last_pts = -1;
                while (!should_quit.load()) {
-                       AVPacket pkt;
-                       unique_ptr<AVPacket, decltype(av_packet_unref) *> pkt_cleanup(
-                               &pkt, av_packet_unref);
-                       av_init_packet(&pkt);
-                       pkt.data = nullptr;
-                       pkt.size = 0;
+                       AVPacketWithDeleter pkt = av_packet_alloc_unique();
+                       pkt->data = nullptr;
+                       pkt->size = 0;
 
                        // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
                        // right now, should_quit will be ignored if it's hung on I/O.
-                       if (av_read_frame(format_ctx.get(), &pkt) != 0) {
+                       if (av_read_frame(format_ctx.get(), pkt.get()) != 0) {
                                break;
                        }
 
-                       AVStream *stream = format_ctx->streams[pkt.stream_index];
+                       AVStream *stream = format_ctx->streams[pkt->stream_index];
                        if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
-                           audio_stream_to_video_stream_idx.count(pkt.stream_index)) {
-                               if ((pkt.size % (sizeof(uint32_t) * 2)) != 0) {
+                           audio_stream_to_video_stream_idx.count(pkt->stream_index)) {
+                               if ((pkt->size % (sizeof(uint32_t) * 2)) != 0) {
                                        fprintf(stderr, "Audio stream %u had a packet of strange length %d, ignoring.\n",
-                                               pkt.stream_index, pkt.size);
+                                               pkt->stream_index, pkt->size);
                                } else {
                                        // TODO: Endianness?
-                                       const uint32_t *begin = (const uint32_t *)pkt.data;
-                                       const uint32_t *end = (const uint32_t *)(pkt.data + pkt.size);
-                                       pending_audio[audio_stream_to_video_stream_idx[pkt.stream_index]].assign(begin, end);
+                                       const uint32_t *begin = (const uint32_t *)pkt->data;
+                                       const uint32_t *end = (const uint32_t *)(pkt->data + pkt->size);
+                                       pending_audio[audio_stream_to_video_stream_idx[pkt->stream_index]].assign(begin, end);
                                }
                        }
 
-                       if (pkt.stream_index >= MAX_STREAMS ||
+                       if (pkt->stream_index >= MAX_STREAMS ||
                            stream->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
                                continue;
                        }
 
-                       ++metric_received_frames[pkt.stream_index];
-                       metric_received_frame_size_bytes.count_event(pkt.size);
+                       ++metric_received_frames[pkt->stream_index];
+                       metric_received_frame_size_bytes.count_event(pkt->size);
 
                        // Convert pts to our own timebase.
                        AVRational stream_timebase = stream->time_base;
-                       int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
+                       int64_t pts = av_rescale_q(pkt->pts, stream_timebase, AVRational{ 1, TIMEBASE });
 
                        // Translate offset into our stream.
                        if (last_pts == -1) {
@@ -572,11 +569,11 @@ void record_thread_func()
                        pts = std::max(pts + pts_offset, start_pts);
 
                        //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
-                       //      pkt.stream_index, pts, pkt.size);
-                       FrameOnDisk frame = write_frame(pkt.stream_index, pts, pkt.data, pkt.size, move(pending_audio[pkt.stream_index]), &db);
+                       //      pkt->stream_index, pts, pkt->size);
+                       FrameOnDisk frame = write_frame(pkt->stream_index, pts, pkt->data, pkt->size, move(pending_audio[pkt->stream_index]), &db);
 
-                       post_to_main_thread([pkt, frame] {
-                               global_mainwindow->display_frame(pkt.stream_index, frame);
+                       post_to_main_thread([stream_index{pkt->stream_index}, frame] {
+                               global_mainwindow->display_frame(stream_index, frame);
                        });
 
                        if (last_pts != -1 && global_flags.slow_down_input) {