[Libav-user] facing an issue with avcodec_decode_subtitle2

srijith vasudevan srijith.vasudevan at gmail.com
Fri Dec 21 17:05:37 CET 2012


Hello,

I am working on an assignment (using C++) to decode all the subtitle
streams present in a m2ts file using libav.

I have a m2ts file with one video stream (video stream index = 0), one
audio stream (audio stream index = 1) and 5 subtitle streams ( stream
indices = 2, 3, 4, 5).

I first noticed the problem when I compared the position (x, y, w, h) I got
from the decoded subtitle against the actual subtitle position from its
control file.

*The details of the actual events*
SUBTITLE_STREAM_INDEX_2
Event1 (X="607" Y="780" START_FRAME="764")

SUBTITLE_STREAM_INDEX_3
Event1 (X="575" Y="780" START_FRAME="764")

SUBTITLE_STREAM_INDEX_4
Event1 (X="701" Y="780" START_FRAME="764")

SUBTITLE_STREAM_INDEX_5
Event1 (X="483" Y="777" START_FRAME="765")

*
   Quote:
Please note Event1 of SUBTITLE_STREAM_INDEX_2, SUBTITLE_STREAM_INDEX_3,
SUBTITLE_STREAM_INDEX_4 start at frame no 764.
  *

When I ran the application, it first decoded Event1 of
SUBTITLE_STREAM_INDEX_4 and I got the position values from the decoded
subtitle as (701, 780, 507, 133).
Next it decoded Event1 of SUBTITLE_STREAM_INDEX_2 and the position values
decoded were as follows (701, 780, 507, 133). Please note the mismatch in
values.
The values were same for decoded subtitle of SUBTITLE_STREAM_INDEX_3.




AS the next step I am decoding the subtitle and generating a png file. This
is when I noticed the issue.

For example I am decoding SUBTITLE_STREAM_INDEX_4 at frame no = 764. I
generated the image using the subtitle->rects[0]->pict.data. This is
correct subtitle. (Assume the dialog to be "hi there").

Now I have another subtitle stream (index = 2) which also starts from the
same frame number (frame no = 764). Now when I generate a png using this
decoded subtitle it has the same subtitle text as the one decoded from
previous index. (the actual dialog is "hola", but the png generated had the
dialog as "hi there").
It is basically giving me the position and data of the subtitle decoded
from the first SUBTITLE_STREAM encountered for a particular frame number.



I am posting the part of code:

   *Code:*
AVPacket packet;
//looping till end of file
{
   av_init_packet(&packet);
    av_read_frame(pFormatContext, &packet);

if((packet.stream_index == m_subtitleStreamIndex[0]) ||
(packet.stream_index == m_subtitleStreamIndex[1]) || (packet.stream_index
== m_subtitleStreamIndex[2]) || (packet.stream_index ==
m_subtitleStreamIndex[3]))
{
   int has_subtitle = 0;
    AVSubtitle* subtitle = new AVSubtitle;
    avcodec_decode_subtitle2(pSubtitleCodecContext, subtitle,
&has_subtitle, &packet);

    if (has_subtitle != 0)
    {
        if (subtitle->rects)
            {
                struct SwsContext *mSwsContext =
sws_getContext(subtitle->rects[0]->w, subtitle->rects[0]->h,
pSubtitleCodecContext->pix_fmt,
                                        subtitle->rects[0]->w,
subtitle->rects[0]->h, PIX_FMT_RGBA,
                                        SWS_BICUBIC, NULL, NULL, NULL);
                AVFrame     *pPicture = AllocatePicture(PIX_FMT_RGBA,
subtitle->rects[0]->w, subtitle->rects[0]->h);

                /* convert to RGB */
                sws_scale(mSwsContext, (const uint8_t **)
subtitle->rects[0]->pict.data, subtitle->rects[0]->pict.linesize, 0,
subtitle->rects[0]->h,
                                        pPicture->data, pPicture->linesize);

                //code to generate png

                av_free(pPicture);
                sws_freeContext(mSwsContext);
            }
        }
   }
    avsubtitle_free(subtitle);
}
}


Could anyone please advice if I am doing something wrong here or suggest a
solution to this problem? I am expecting your priceless guidance and help.

Thanks,
Srijith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20121221/ec3fde53/attachment.html>


More information about the Libav-user mailing list