Przeglądaj źródła

fix pts offset bug

DevYK 3 lat temu
rodzic
commit
0ac023a8c2

+ 16 - 8
src/ffmpeg/av_decode.cpp

@@ -89,11 +89,19 @@ static int av_decode(struct DecoderContext *pContext, AVPacket *pPacket, enum AV
         else
             pts = out_frame->pts * (1000 *
                                     (av_q2d(pContext->avformatContext->streams[pContext->st_index[type]]->time_base)));
-        if (pContext->end_time > 0 && pts > pContext->end_time && type == AVMEDIA_TYPE_VIDEO){
+
+
+        int64_t offset = 0;
+        if (pContext->offset != AV_NOPTS_VALUE) {
+            offset += pContext->offset / 1000;
+            pts -= offset;
+        }
+
+        if (pContext->end_time > 0 && pts > pContext->end_time && type == AVMEDIA_TYPE_VIDEO) {
             av_frame_free(&out_frame);
             return -4;
         }
-        if (pContext->end_time > 0 && pts > pContext->end_time && type == AVMEDIA_TYPE_AUDIO){
+        if (pContext->end_time > 0 && pts > pContext->end_time && type == AVMEDIA_TYPE_AUDIO) {
             av_frame_free(&out_frame);
             return 0;
         }
@@ -265,18 +273,18 @@ long initDecoder(const char *url, int force_Iframe, DisableMediaType disableMedi
         dctx->video_queue = new BlockQueue<AVFrame *>(dctx->fps);
     }
 
-    seekToMs(dctx);
+    seekToMs(dctx, dctx->offset);
     return ret == 0 ? (long) dctx : ret;
 }
 
-void seekToMs(DecoderContext *dctx) {
+void seekToMs(DecoderContext *dctx, int64_t offset) {
     int64_t timestamp = dctx->start_time * 1000;
-    if (dctx->offset != AV_NOPTS_VALUE)
-        timestamp += dctx->offset;
+    if (offset != AV_NOPTS_VALUE)
+        timestamp += offset;
     if (timestamp > 0) {
         avformat_flush(dctx->avformatContext);
         int ret = avformat_seek_file(dctx->avformatContext, -1, INT64_MIN, timestamp,
-                                     INT64_MAX,
+                                     timestamp,
                                      AVSEEK_FLAG_BACKWARD);
         if (ret >= 0) {
             if (dctx->videoCodecContext)
@@ -358,7 +366,7 @@ void *av_read_decode_thread(void *pVoid) {
                 ctx->decode_frame_ms = 0;
                 if (cur_item->startTimeMs > 0) {
                     ctx->start_time = cur_item->startTimeMs;
-                    seekToMs(ctx);
+                    seekToMs(ctx, ctx->offset);
                 }
                 if (cur_item->endTimeMs > 0)
                     ctx->end_time = cur_item->endTimeMs;

+ 1 - 1
src/ffmpeg/av_decode.h

@@ -120,7 +120,7 @@ typedef struct DecoderContext {
 /*最简单的解码器实现*/
 long initDecoder(const char *url, int force_Iframe, DisableMediaType disableMediaType);
 
-void seekToMs(DecoderContext *dctx);
+void seekToMs(DecoderContext *dctx,int64_t offset);
 
 int av_read_decode_frame(long decodec_id);
 

+ 8 - 7
src/video_similarity_comparison.cpp

@@ -58,7 +58,7 @@ static int *list_sim_compare(std::vector<ImageHashModel *> a, std::vector<ImageH
 static void run(){
 
     const char *input_json = "{\n"
-                             "  \"videoPath\": \"http://rescdn.yishihui.com/longvideo/transcode/video/qa/20220314/6267824rwLv7IntDRwKxLG7y6.m3u8\",\n"
+                             "  \"videoPath\": \"http://rescdn.yishihui.com/longvideo/transcode/crawler/video/prod/20210708/VIDEO_XIGUA_6981987670008267267.m3u8\",\n"
                              "  \"clips\": [\n"
 //                             "    {\n"
 //                             "      \"startTimeMs\": 119900,\n"
@@ -83,14 +83,14 @@ static void run(){
 //                             "    {\n"
 //                             "      \"startTimeMs\": 719400,\n"
 //                             "      \"endTimeMs\": 720400\n"
+//                             "    },\n"
+//                             "    {\n"
+//                             "      \"startTimeMs\": 87200,\n"
+//                             "      \"endTimeMs\": 88200\n"
 //                             "    },\n"
                              "    {\n"
-                             "      \"startTimeMs\": 839300,\n"
-                             "      \"endTimeMs\": 840300\n"
-                             "    },\n"
-                             "    {\n"
-                             "      \"startTimeMs\": 959200,\n"
-                             "      \"endTimeMs\": 960200\n"
+                             "      \"startTimeMs\": 87200,\n"
+                             "      \"endTimeMs\": 88200\n"
                              "    }\n"
                              "  ]\n"
                              "}";
@@ -110,6 +110,7 @@ static void run(){
  */
 int main(int argc, char *argv[]) {
     run();
+    return 1;
 
 //    const char *out_json = "{\n"
 //                           "    \"videoPath\": \"\\\"/root/data/xxx.mp4\\\"\",\n"