[FFmpeg-devel] [PATCH 1/2] allow passing subtitles header between decoder and encoder

Aurelien Jacobs aurel
Sat Oct 16 17:31:26 CEST 2010


---
 ffmpeg.c             |   11 +++++++++++
 libavcodec/avcodec.h |    8 ++++++++
 libavcodec/utils.c   |    2 ++
 libavformat/utils.c  |    4 ++++
 4 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 4f59b2e..7622ccc 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2365,6 +2365,7 @@ static int transcode(AVFormatContext **output_files,
         ost = ost_table[i];
         if (ost->encoding_needed) {
             AVCodec *codec = i < nb_output_codecs ? output_codecs[i] : NULL;
+            AVCodecContext *dec = ist_table[ost->source_index]->st->codec;
             if (!codec)
                 codec = avcodec_find_encoder(ost->st->codec->codec_id);
             if (!codec) {
@@ -2373,6 +2374,15 @@ static int transcode(AVFormatContext **output_files,
                 ret = AVERROR(EINVAL);
                 goto dump_format;
             }
+            if (dec->subtitle_header) {
+                ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
+                if (!ost->st->codec->subtitle_header) {
+                    ret = AVERROR(ENOMEM);
+                    goto dump_format;
+                }
+                memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
+                ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
+            }
             if (avcodec_open(ost->st->codec, codec) < 0) {
                 snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height",
                         ost->file_index, ost->index);
@@ -2728,6 +2738,7 @@ static int transcode(AVFormatContext **output_files,
                 }
                 av_fifo_free(ost->fifo); /* works even if fifo is not
                                              initialized but set to zero */
+                av_freep(&ost->st->codec->subtitle_header);
                 av_free(ost->pict_tmp.data[0]);
                 if (ost->video_resample)
                     sws_freeContext(ost->img_resample_ctx);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 4bddbaa..ab3cbd9 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2744,6 +2744,14 @@ typedef struct AVCodecContext {
      * - decoding: unused
      */
     int lpc_passes;
+
+    /**
+     * Header containing style information for text subtitles.
+     * - encoding: Set/allocated/freed by user (before avcodec_open())
+     * - decoding: Set/allocated/freed by libavcodec (by avcodec_open())
+     */
+    uint8_t *subtitle_header;
+    int subtitle_header_size;
 } AVCodecContext;
 
 /**
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index ffd34ee..3b98854 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -742,6 +742,8 @@ av_cold int avcodec_close(AVCodecContext *avctx)
     av_freep(&avctx->priv_data);
     if(avctx->codec && avctx->codec->encode)
         av_freep(&avctx->extradata);
+    if (avctx->codec && avctx->codec->encode)
+        av_freep(&avctx->subtitle_header);
     avctx->codec = NULL;
     entangled_thread_counter--;
 
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 8a08557..a137e69 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2022,6 +2022,9 @@ static int has_codec_parameters(AVCodecContext *enc)
     case AVMEDIA_TYPE_VIDEO:
         val = enc->width && enc->pix_fmt != PIX_FMT_NONE;
         break;
+    case AVMEDIA_TYPE_SUBTITLE:
+        val = !!enc->subtitle_header;
+        break;
     default:
         val = 1;
         break;
@@ -2467,6 +2470,7 @@ void av_close_input_stream(AVFormatContext *s)
         av_metadata_free(&st->metadata);
         av_free(st->index_entries);
         av_free(st->codec->extradata);
+        av_free(st->codec->subtitle_header);
         av_free(st->codec);
 #if FF_API_OLD_METADATA
         av_free(st->filename);
-- 
1.7.1




More information about the ffmpeg-devel mailing list