Bug 694691 - OpenJPEG always calls malloc/free
Summary: OpenJPEG always calls malloc/free
Status: RESOLVED LATER
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: JPX/JBIG2 encode/decode (show other bugs)
Version: master
Hardware: PC Windows 7
: P4 normal
Assignee: Shailesh Mistry
URL:
Keywords: bountiable
Depends on:
Blocks:
 
Reported: 2013-10-01 09:39 UTC by Robin Watts
Modified: 2023-05-31 09:03 UTC (History)
2 users (show)

See Also:
Customer:
Word Size: ---


Attachments
Add GS memory management to OpenJPEG2.1 (463.71 KB, patch)
2014-06-13 15:57 UTC, Shailesh Mistry
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Robin Watts 2013-10-01 09:39:14 UTC
In gs (and mupdf) we have our own memory allocation functions; we would like all the libraries we call to use these function rather than call vanilla malloc/free.

Problem 1: There is no way to stop OpenJPEG calling malloc and free directly.

This can be solved with a (fairly trivial) tweak to opj_malloc.h so that when built with a suitable predefine (OPJ_USER_MALLOC or something similar), calls to opj_malloc etc are NOT #defined down to malloc, and versions of opj_malloc can be supplied by the user.

This would be a fairly simple and harmless patch that I hope we could persuade the OpenJPEG developers to take on, but it might be problematic for shared library builds of OpenJPEG.

Problem 2: If user supplied allocators are to be called, then it's probable that those allocators would need to take some sort of argument (in gs, they would need a gs_memory_t, and in mupdf an fz_context *). There is no scope to add this without breaking the OpenJPEG API.

Some discussion therefore needs to take place with the OpenJPEG developers to try to see if they are amenable to some sort of solution for this, together with the API changes it would require.
Comment 1 Robin Watts 2013-10-01 09:39:55 UTC
Bountiable to Shelly as he has a relationship with the openjpeg developers, I believe.
Comment 2 Shailesh Mistry 2013-10-08 11:31:06 UTC
I emailed an outline of this proposal to my contact on the 2/Oct/2013 but have not heard back yet.

I will send a reminder to them around the 16/Oct.
Comment 3 Shailesh Mistry 2013-10-21 14:23:08 UTC
I nudged the OpenJPEG2 team for an update and got the following response :-

"No, nothing done on openjpeg lately sorry :( And I cannot even give
you a clear vision of my agenda in the coming months... I am CCing the
team in case someone else has some time in the coming weeks."

Personally, I think our best bet right now is to map out our possible changes and ask them to review it, set a time limit for a response and then continue preferably with but possibly without their feedback.

Do you guys have any thoughts on this?

Should I continue looking into this or do one of you want to step in to add some weight to the discussions?
Comment 4 Shailesh Mistry 2013-11-19 13:35:19 UTC
Analysis of the existing OpenJPEG2 code has highlighted a number of issues :-

1) The main context (opj_codec_private_t) is defined in openjpeg.c and hence hidden from the rest of the source files. A "void *" is used throughout to access the context but all function calls that need to know the internal structure pass through openjpeg.c.

2) The OpenJPEG2 memory allocation calls make no provision for a pointer to our internal memory.

3) A number of the memory allocation calls are nested very deep and nowhere near the high level context.


To overcome these problems will take a number of staged updates which the OpenJPEG2 team may be more likely to accept for merging in. The stages should include :-

1) Make opj_codec_private_t available through an include file.

2) Propagate the main context to the lower function calls.

3) Add a memory pointer to the main context for our internal memory.

4) Make all memory function calls access our internal memory.


Stage 1 has been submitted to OpenJPEG2 for review and I will update this bug when I hear back from them.
Comment 5 Ray Johnston 2013-11-19 16:05:34 UTC
While this approach would provide a way to set memory function callouts:
> 1) Make opj_codec_private_t available through an include file.

If the OpenJPEG developers object, then providing a function that sets the
memory callouts (alloc, free, realloc, ...) would suffice as well, correct ?

(similar to gsapi_set_stdio).

Of course, all of the memory callout functions must pass a 'void *' which
the provider functions can use, as well as the 'standard' parameters.

Just a suggestion.
Comment 6 Shailesh Mistry 2013-12-04 10:26:18 UTC
I have not had a reply from the OpenJPEG2 team so I emailed them again on the 2/Dec asking for an update.
Comment 7 Shailesh Mistry 2014-01-12 08:07:39 UTC
The OpenJPEG team requested that this bug is opened in their own system. The first stage changes can now be tracked at https://code.google.com/p/openjpeg/issues/detail?id=253
Comment 8 Henry Stiles 2014-02-23 19:17:25 UTC
(In reply to comment #7)
> The OpenJPEG team requested that this bug is opened in their own system. The
> first stage changes can now be tracked at
> https://code.google.com/p/openjpeg/issues/detail?id=253

There was a response 1/13/2014 which needs a follow up, let me know if you are going to do that, Shelly.  Thanks.
Comment 9 Shailesh Mistry 2014-03-04 14:53:49 UTC
The openjpeg team are in the process of making OpenJPEG 2.1.0 and I have updated their bug report today with more details.
Comment 10 Shailesh Mistry 2014-03-13 16:37:58 UTC
The first patch has been accepted by the OpenJPEG team. I will now progress onto the second patch.
Comment 11 Shailesh Mistry 2014-06-13 15:57:59 UTC
Created attachment 10990 [details]
Add GS memory management to OpenJPEG2.1

The second patch has now been submitted under a new bug report (https://code.google.com/p/openjpeg/issues/detail?id=356) to the OpenJPEG team.

This has been regression tested and checked into git (1415c32878bcdaf6b9ec83718c34e50607d0eff2).
Comment 12 Shailesh Mistry 2014-07-16 13:16:52 UTC
I contacted the OpenJPEG team for an update and they have confirmed that this bug has now changed status to "accepted" and been upgraded from medium to high priority.
Comment 13 Shailesh Mistry 2014-09-06 09:53:53 UTC
The OpenJPEG team contacted me on the 17th July with concerns about the depth of changes needed for the memory management. 

I sent them an update on the 21st July but got no response.

I sent another email on the 18th of Aug but still got no response.

I then sent another email on the 26th Aug but have not heard anything yet.

I will wait a few more days and then email them again. Should I emphasise that we are considering forking the code if we do not hear back from them soon?
Comment 14 Shailesh Mistry 2014-10-17 11:19:10 UTC
The OpenJPEG team have provided 2 alternative patches for this work and I am currently reviewing them.
Comment 15 Shailesh Mistry 2015-02-02 14:14:24 UTC
The updated patch from the OpenJPEG team has been regression tested and worked fine. I have informed them of the test results and await their decision.
Comment 16 Shailesh Mistry 2015-02-14 09:27:14 UTC
The patch from the OpenJPEG team has been regression tested and highlighted that they have not implemented a bug fix sent to them by us in https://code.google.com/p/openjpeg/issues/detail?id=235 

Without this patch a number of the files show slightly blurred images compared to our main HEAD.

I have updated the OpenJPEG team and await their reply.
Comment 17 Shailesh Mistry 2015-03-07 05:13:30 UTC
I have nudged the OpenJPEG team again today to try and get some movement on this bug.
Comment 18 Shailesh Mistry 2015-09-12 09:26:39 UTC
The OpenJPEG team requested that I fork their GitHub code and make a pull request. I have updated the code, regression tested it in Ghostscript and made the pull request today.
Comment 19 Henry Stiles 2019-01-30 22:45:30 UTC
Hi Shelly, what is the status of this bug?  It looks like gs is not using malloc/free for openjpeg now but it looks like there was more to this.
Comment 20 Henry Stiles 2019-01-31 00:03:44 UTC
We also noticed a global variable "opj_memory", not sure if that is workaround until the openjpeg group can make changes in their code.  If it is not going to go away soon we'll add a mutex.
Comment 21 Shailesh Mistry 2019-01-31 19:05:27 UTC
As of May 2017 they had mainly accepted the patch which worked with the new memory manager code and the existing API but were still requesting more changes with no commitment to actually include it. (See https://github.com/uclouvain/openjpeg/pull/582 )

However, the OpenJpeg team wanted to know what would happen if someone switched between the old and new API part way through processing a file! They boiled it down to either having both API present for a short while or going the full way and switching over to the new one.

Bottom line is they only want to do a full change over and ditch the old API but they cant justify moving to the new one!

Not sure how we progress from here.